ASP. NET core uses session:
After creating a new ASP. NET core application, to use session middleware, you need to perform three steps in Startup.cs:
1. Use a service that implements the Idistributedcache interface to enable memory caching. (for example, using a memory cache)
This step needs to be used before the addsession () call.
2. Call the Addsession method
3. Use the Usesession callback (Usesession need to be called before the Usemvc () method)
The specific code is as follows:
Public classStartup { PublicStartup (iconfiguration configuration) {Configuration=configuration; } PublicIConfiguration Configuration {Get; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {services. Adddistributedmemorycache (); Services. Addsession (Options={options. Cookie.name=". Adventureworks.session"; Options. IdleTimeout= Timespan.fromseconds (Ten)//Set the expiration time of the session options. Cookie.httponly=true; } ); Services. Addmvc (); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } app. Usesession (); App. Usemvc (); }}
How ASP. NET Core uses session