The Configureservices method configuration in the Startup.cs file:
#region Session Memory Cache
Services. configure<cookiepolicyoptions> (options =
{
Options. checkconsentneeded = Context = true;
Options. Minimumsamesitepolicy = Samesitemode.none;
});
Enable memory cache (This step needs to be used before the addsession () call)
Services. Adddistributedmemorycache ();//You must add memory before enabling session
Services. Addsession ();
Services. Addsession (options =
{
Options. Cookie.name = ". Adventureworks.session ";
Options. IdleTimeout = Timespan.fromseconds (2000);//Setting the session Expiration time
Options. cookie.httponly = true;//settings in the browser cannot get the value of the cookie via JS
});
#endregion
In the Configure method:
App. Usesession ();//usesession Configuration before USEMVC
Used in the controller:
Save: HttpContext.Session.SetString ("Code", "123456");
Fetch: HttpContext.Session.GetString ("code");
Options. checkconsentneeded = Context = true; To be set to false, otherwise the access session ID inconsistency is inverted.
ASP. NET core Webapi Session memory cache