ASP Net Core 2.2 Session lost after redirect

时间:2019-04-23 15:12:58

标签: c# asp.net-core session-cookies session-state

I have the following session config in Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.Cookie.HttpOnly = true;

            // Make the session cookie essential
            options.Cookie.IsEssential = true;
        });
     }

Everything works fine and the session keeps it Session Id and data. I'm putting some booking info there. Then I redirect a user to payment page. After successful payment the user is redirected back to my site via POST to PaymentController Success action.

Obviously, this POST request from payment platform doesn't contain session cookies, so I just do RedirectToAction("ContinueBooking","Payment") inside PaymentController Success action.

After redirect to action - the browser sends session cookies but the session seems to be lost at this time and a new session Id assigned, all data is lost. Is there any way to prevent this ? Or should I just use database to store data between redirects in my case (i can bypass some parameters through payment terminal)?

1 个答案:

答案 0 :(得分:0)

如果两个应用程序都在同一台服务器上,那么它们都将是相同的cookie,并且第一个cookie将被付款应用程序的会话cookie覆盖。为了避免这种情况,请自定义Cookie名称:

services.AddSession(opt => { opt.Cookie.Name = "MySessionCookie"; });