1. Use Session
1.1 Configuration in Status.cs
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDistributedMemoryCache(); services.AddSession(options => { options.Cookie.Name = "haos.Session"; options.IdleTimeout = TimeSpan.FromHours( 1 ); // Set the expiration time of the session options.Cookie.HttpOnly = true ; options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseSession(); // call app.UseMvc(routes => before UseMvc { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
1.2 The session must be in the browser for the call to be effective.
2. Cookie authentication
2.1 Configuration in Status.cs
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddAuthentication("haos") .AddCookie("haos", (option) => { option.LoginPath = "/Account/login"; option.LogoutPath = "/Account/logout"; option.ExpireTimeSpan = TimeSpan.FromDays(1); option.AccessDeniedPath = new PathString("/Account/Login"); option.Cookie = new CookieBuilder() { Name = "haos.develop" }; }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseAuthentication(); }
2.2 Implement the login method
public async Task LoginSignIn(object member) { var identity = new Claim[] { new Claim(ClaimTypes.Name, ""), new Claim(ClaimTypes.MobilePhone, ""), new Claim(ClaimTypes.PrimarySid,""), new Claim("id",""), }; await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(identity, "haos")), new AuthenticationProperties() { ExpiresUtc = DateTime.UtcNow.Add(TimeSpan.FromDays(7)), IsPersistent = true }); }