gpt4 book ai didi

asp.net - .NET Core 2 CookieAuthentication 忽略过期时间跨度

转载 作者:行者123 更新时间:2023-12-01 12:12:39 25 4
gpt4 key购买 nike

我正在使用 CookieAuthentication 开发 .NET Core 2.1 Web 应用程序.出于某种原因设置 ExpireTimeSpanCookie.ExpirationCookieAuthenticationOptions对象对 Cookie 生存期没有影响。 Chrome 始终显示相同的到期日期 1969-12-31T23:59:59.000Z .所以在关闭浏览器窗口后,cookie 消失了。

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/Account/Login/");
options.AccessDeniedPath = new PathString("/Account/Login/");
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
options.Cookie.Expiration = TimeSpan.FromDays(14);
options.ExpireTimeSpan = TimeSpan.FromDays(14);
});

services.AddMvc(options =>
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
});

services.AddAntiforgery(options => options.HeaderName = "X-CSRF-TOKEN");
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}

var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".tag"] = "riot/tag";

app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = provider
});

app.UseAuthentication();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

在登录时,我正在使用此代码

ClaimsPrincipal user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userId.Value.ToString()) }, CookieAuthenticationDefaults.AuthenticationScheme));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);

我试过把 services.AddMvc之前 services.AddAuthentication但这没什么区别。我也试过 services.ConfigureApplicationCookie之后 services.AddAuthentication就像在这个答案中 Cookie expiry in ASP.NET Core 2.0 with Identity

我错过了什么?

最佳答案

Chrome 中的过期日期代表浏览器中 cookie 的生命周期,而不是 token 的超时时间。将 Identity Server 4 与 ASP.NET Identity 一起使用时,此处起作用的是 Identity Server 的 cookie 超时。客户端 token 过期后,用户将根据 Identity Server 重新进行身份验证,并且由于该 token 尚未过期,客户端 token 将被更新。要在 Identity Server 上设置过期时间,您必须在 Identity Server Startup.cs 中添加 ConfigureApplicationCookie 中间件,如下所示:

services.AddAuthentication();

services.ConfigureApplicationCookie(options =>
{
options.Cookie.Expiration = TimeSpan.FromDays(14);
options.ExpireTimeSpan = TimeSpan.FromDays(14);
options.SlidingExpiration = false;
});

services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);
.net core 3.1 的更新(不再需要将 cooke.expiration 作为单独的选项):
services.AddAuthentication();

services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(14);
options.SlidingExpiration = false;
});

services.AddMvc();

关于asp.net - .NET Core 2 CookieAuthentication 忽略过期时间跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com