6

我在尝试在我的 cookie 中设置 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();
    }

但这就是我得到它的方式

在此处输入图像描述

4

2 回答 2

7

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

控制存储在 cookie 中的身份验证票证从创建之日起保持有效的时间。过期信息存储在受保护的 cookie 票证中。因此,即使在浏览器应该清除它之后将过期的 cookie 传递给服务器,它也会被忽略。

这与 的值不同,后者指定浏览器保留 cookie 的时间。

文档

您想使用Expiration属性上的Cookie属性来控制 cookie 过期。

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属性。

于 2020-05-28T11:01:18.843 回答
1

我在 .net core 3.1 中有一个应用程序,我的 ConfigureServices 如下所示:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
    //options.Cookie = new CookieBuilder() { Name = "EcomAuth" };
    options.LoginPath = "/Account/Login/";
    options.AccessDeniedPath = "/Account/AccessDenied";
    options.LogoutPath = "/Account/Logout";
    options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
});

对于一些错误,当我设置 cookie 名称时,代码停止工作,所以这一行被注释掉了。这是我的登录操作

List<Claim> claims = new List<Claim>
{
    new Claim(ClaimTypes.Name, user.Name, ClaimValueTypes.String),
    new Claim(ClaimTypes.Role, userType.Name, ClaimValueTypes.String),
    new Claim("Idusuario",user.IdUser.ToString(), ClaimValueTypes.String),
};

ClaimsIdentity identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

var authProperties = new AuthenticationProperties
{
    AllowRefresh = true,
    ExpiresUtc = DateTime.UtcNow.AddMinutes(120),
    IsPersistent = true,
    RedirectUri = "https://localhost:44318/Account/Logout"
};

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), authProperties);

它对我来说很好。

于 2020-05-28T10:48:39.777 回答