在使用ASP.NET Core 2.0的时候使用下面的代码实现身份验证,设置了ExpireTimeSpan不起作用始终是会话结束过期
services.AddCookieAuthentication(options => { options.ExpireTimeSpan = TimeSpan.FromDays(2); }); services.AddMvc(options => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); options.Filters.Add(new AuthorizeFilter(policy)); });
还需要在登录时设置AuthenticationProperties,参考代码:
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, loginName) }, "Basic"); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); await context.Authentication.SignInAsync(_cookieAuthOptions.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties { IsPersistent = isPersistent, ExpiresUtc = DateTimeOffset.Now.Add(_cookieAuthOptions.ExpireTimeSpan) });
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.AddDays(7) });
还是无效呀
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.AddDays(7) });
还是无效呀