标签:use mes site 扩展方法 microsoft res ack options text
PM> Install-Package Microsoft.AspNetCore.Session -Version 2.2.0
也可直接在项目工程文件(*.csproj)中添加如下代码达到添加Session依赖的目的
1 <ItemGroup> 2 <PackageReference Include="Microsoft.AspNetCore.Session" Version="2.2.0" /> 3 </ItemGroup>
在Startup类的ConfigureServices()方法中调用AddSession()服务:
1 public void ConfigureServices(IServiceCollection services) 2 { 3 services.AddSession(); 4 }
在Startup类的Configure()方法中添加UseSession()中间件:
1 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 2 { 3 app.UseSession(); 4 }
防止页面切换后Session ID改变,Seesion失效
1 public void ConfigureServices(IServiceCollection services) 2 { 3 services.Configure<CookiePolicyOptions>(options => 4 { 5 // This lambda determines whether user consent for non-essential cookies is needed for a given request. 6 options.CheckConsentNeeded = context => false;//默认为true,改为false 7 options.MinimumSameSitePolicy = SameSiteMode.None; 8 }); 9 }
Session写入
1 HttpContext.Session.SetString("key", "value");
Session读取
1 HttpContext.Session.GetString("key");
扩展方法
1 /// <summary> 2 /// Session写入 3 /// </summary> 4 /// <param name="key">键</param> 5 /// <param name="value">值</param> 6 protected void SetSession(string key, string value) 7 { 8 HttpContext.Session.SetString(key, value); 9 } 10 11 /// <summary> 12 /// Session读取 13 /// </summary> 14 /// <param name="key">键</param> 15 /// <returns>返回对应的值</returns> 16 protected string GetSession(string key) 17 { 18 var value = HttpContext.Session.GetString(key); 19 if (string.IsNullOrEmpty(value)) 20 value = string.Empty; 21 return value; 22 }
标签:use mes site 扩展方法 microsoft res ack options text
原文地址:https://www.cnblogs.com/gygg/p/11290082.html