gpt4 book ai didi

c# - Cookie.ExpireTimeSpan 被忽略并在 CookieAuthentication 中设置为 Session

转载 作者:行者123 更新时间:2023-12-04 12:10:15 28 4
gpt4 key购买 nike

我在尝试设置 时遇到问题过期 我的 cookie 时间 CookieAuthentication ,看来ExpireTimeSpan只是被忽略,当我在浏览器中获取 cookie 时,它​​的过期时间设置为 Session ..

我使用的是带有 .NET Core 3.1 的 c# 8.0,这是我的 ConfigureService代码:

    public void ConfigureServices(IServiceCollection services)
{

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => {
options.Cookie.Name = "authToken";
options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
options.Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = (context) =>
{
context.HttpContext.Response.Redirect("https://example.com/test/expired.html");
return Task.CompletedTask;
}
};
});
services.AddControllers();
}

但这就是我得到它的方式

enter image description here

最佳答案

options.ExpireTimeSpan = TimeSpan.FromMinutes(120);指示身份验证票本身的有效时间。

Controls how much time the authentication ticket stored in the cookie will remain valid from the point it is created The expiration information is stored in the protected cookie ticket. Because of that an expired cookie will be ignored even if it is passed to the server after the browser should have purged it.

This is separate from the value of , which specifies how long the browser will keep the cookie.

Docs



您想使用 Expiration 控制 cookie 的过期时间属性(property)在 Cookie属性(property)。
public void ConfigureServices(IServiceCollection services)
{

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => {
options.Cookie.Name = "authToken";
/// control cookie expiration
options.Cookie.Expiration = TimeSpan.FromMinutes(120);
options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
options.Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = (context) =>
{
context.HttpContext.Response.Redirect("https://example.com/test/expired.html");
return Task.CompletedTask;
}
};
});
services.AddControllers();
}

或者,您可以设置 MaxAge属性(property)也是。

关于c# - Cookie.ExpireTimeSpan 被忽略并在 CookieAuthentication 中设置为 Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62062215/

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