要解决ASP.NET Core中Cookie身份验证滑动过期时间不起作用的问题,你可以按照以下步骤进行操作:
services.Configure(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
});
app.UseAuthentication();
public class HomeController : Controller
{
public async Task Index()
{
var authenticationProperties = new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddMinutes(30)
};
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(), authenticationProperties);
return View();
}
}
以上代码将设置滑动过期时间为30分钟。你可以根据需要进行调整。
通过以上步骤,你应该能够解决ASP.NET Core中Cookie身份验证滑动过期时间不起作用的问题。