标签:dex nbu void ONBUILD 启用 net div page nbsp
在Core2.1中启用Session
Startup.cs文件进行配置
ConfigureServices方法的配置
在 services.AddMvc(....);这句上面加上
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
这里面的30是指Session的生命周期为30分钟
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
Configure方法中的配置
在 app.UseMvc(...);的上面加上
app.UseSession();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
在使用Session的页面中添加using引用
using Microsoft.AspNetCore.Http;
Microsoft.AspNetCore.Http的方法
namespace Microsoft.AspNetCore.Http { public static class SessionExtensions { public static byte[] Get(this ISession session, string key); public static int? GetInt32(this ISession session, string key); public static string GetString(this ISession session, string key); public static void SetInt32(this ISession session, string key, int value); public static void SetString(this ISession session, string key, string value); } }
Get为获取值
HttpContext.Session.GetString("username");
Set为设置
HttpContext.Session.SetString("username", "admin");
标签:dex nbu void ONBUILD 启用 net div page nbsp
原文地址:https://www.cnblogs.com/tangjiaoshu/p/10067884.html