ASP.NET core 2.1 session

别说谁变了你拦得住时间么 提交于 2019-11-29 01:40:54

In the ConfigureServices() method of the Startup class, Set options.CheckConsentNeeded = context => false; as follows:

services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => false; // Default is true, make it false
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

The solution is to mark the session cookie as essential.

public void ConfigureServices(IServiceCollection services)
{
    //...
    services.AddSession(opt =>
    {
        opt.Cookie.IsEssential = true;
    });
    //...
}

The documentation about the flag states:

Indicates if this cookie is essential for the application to function correctly. If true then consent policy checks may be bypassed. The default value is false.

This will keep the cookie policy options intact and the session is still working as expected because CookiePolicyOptions.CheckConsentNeeded only affects non-essential cookies.

The default distributed cache store in ASP.NET Core is in-memory. Since sessions use distributed cache, that means your session store is also in-memory. Things stored in memory are process-bound, so if the process terminates, everything stored in memory goes along with it. Finally, when you stop debugging, the application process is terminated. That then means every time you start and stop debugging, you have an entirely new session store.

There's a couple of routes you can take. First, if you just want to run the site, without debugging it, you can use CTRL+F5. This will kick off IIS Express and load your web app, without starting all the debugging machinery along with it. You can then proceed to make as many requests as you like, and it will all hit the same process (meaning your session store will be intact). This is great for doing the frontend side of development, as you can modify your Razor views, CSS, JS, etc. and see those changes without have to stop and start debugging again. However, if you make any C# code changes (class, controller, etc.), Visual Studio will kick off a build, which will terminate the application and then restart it. Your site keeps running as if nothing happened, but anything stored in-memory, including your sessions will be gone. It's at least better than debugging constantly, though.

Second, you can simply use a persistent store in development, as well (you should already be setup to use a persistent store in production, so fix that ASAP, if not). You can use something like SQL Server or Redis in development, just like you would in production. The SQL store can be added to your existing development database, so you don't actually need to install SQL Server. You can also install a local copy of Redis and just run it off of localhost, if you prefer that route. With either approach, your distributed cache, and your sessions along with it, will be stored in something external to the application, so starting and stopping your application will have no effect on what's stored there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!