Empty cookie and session after stripe checkout redirection

76 views Asked by At

Environment:

  • .NET Core 5
  • ASP.NET Core 5 MVC + Web API
  • EC2 + stripe

I have a web application built with ASP.NET Core 5 MVC, including a Web API project, and other class libraries. The application is deployed on EC2 and uses Stripe for payments.

When a user goes to the checkout page, it redirects to either SuccessUrl (if the payment was successful) or CancelUrl. Both URLs redirect to the ASP.NET Core MVC project. However, sometimes the user gets logged out because the session and cookie are not present.

Interestingly, this issue doesn’t occur on our testing website where the API and MVC projects are on the same server. However, on our production website, the API and MVC projects are on different servers, and the problem is only here.

I’m also noticing that this problem occurs on all browsers except Edge.

Could deploying on a different server for the production causing this issue? and ow can I maintain the session and cookie after the redirect?

I tried to change the same site from none to lax but still the same problem.

Here is the configuration of the session and cookies:

services.AddDistributedMemoryCache();
services.AddRazorPages().AddRazorRuntimeCompilation();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddWebBaseContexts();

services.Configure<CookiePolicyOptions>(options =>
     {
         options.CheckConsentNeeded = context => false;
         options.MinimumSameSitePolicy = SameSiteMode.lax;
         options.Secure = CookieSecurePolicy.Always;
     });

services.AddHttpClient();

#region "Session init"
services.AddDistributedMemoryCache();
services.AddSession(options =>
     {
         options.Cookie.Name = ".AspNetCore.Session.MakMak.Client";
         options.IdleTimeout = TimeSpan.FromDays(150);
         options.Cookie.IsEssential = true;
         options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
     });
#endregion "Session init"

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
     .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
         options =>
         {
             options.LoginPath = new PathString("/");
         }
     );

app.UseAuthentication();
app.UseSession();

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();
app.UseCookiePolicy();
// app.UseSession();

app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
0

There are 0 answers