ASP.NET Core 2.0中使用Identity的Cookie过期时间

5

环境: ASP.NET Core 2.0, 使用cookie的身份验证。

Startup.ConfigureServices() 中有以下代码:

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

第一个来自CookieAuthenticationOptions,第二个来自CookieBuilder。文档还提到Microsoft.AspNetCore.Http.CookieOptions.Expires(尽管它在该lambda中不可用)。
这些之间有什么区别?在Core2中设置过期时间的正确方法是什么?
2个回答

4
下面是我在测试应用程序中用来设置 cookie 过期的代码。
public class Startup
{
    ...

    // This method gets called by the runtime. Use this method to add services to the container
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        ...

        ...  // before services.AddMvc();!
        services.AddAuthentication().AddCookie(options => {
            options.Cookie.Expiration = TimeSpan.FromDays(14);
            options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
            options.Cookie.Name = "MyCookieName";
            options.LoginPath = "/Account/Login";
            options.AccessDeniedPath = "/Account/Forbidden";
        });

        // OR Perhaps, this could be what you need
        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.Expiration = TimeSpan.FromDays(150);
            options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
            options.Cookie.Name = "MyCookieName";
            options.LoginPath = "/Account/Login";
            options.AccessDeniedPath = "/Account/Forbidden";
        });
        ...
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ... // before app.UseMvc();!
        app.UseAuthentication();
        // WAS -> app.UseCookieAuthentication();
        ...
    }
    ...
}

我认为这可以帮助你朝着正确的方向前进。

这对我有用,而且我还没有注意到任何问题。虽然,自 Core 2.0 RTM 发布以来只过了几周。 :)

希望这能帮到你。


1
我正在使用options.ExpireTimeSpan = TimeSpan.FromDays(14);,正如文档站点上的某一页所述。但是存在三种不同的方法让我相信它们之间存在差异。身份验证一直很混乱(并且从未得到适当的记录),因此我非常想了解更多关于这个问题的信息。感谢您的真实世界确认,它有所帮助!我怀疑在使用Identity时和不使用Identity时使用cookie存在差异-您正在使用不带Identity的cookie,而我使用带Identity的cookie,因此也许在那里有一些线索... - grokky
这只是一个小问题,你说得对...不过看起来你添加了两次cookie支持。至少根据“带/不带Identity的cookie”文档页面所述。它说你只需要为基于cookie的身份验证AddIdentity(),而不需要.AddAuthentication().AddCookie(...) - grokky
我可以看出来,它看起来像是我重复添加了cookie支持。我之所以这样做是因为在AddIdentity选项中找不到options.Cookie.Expiration。我没有收到任何投诉或错误信息,所以我就让它保留了下来。我可能会在Identity代码中查找,看看这可能会引起什么影响(如果有的话)。 - R. Richards
我在代码中添加了一个 services.ConfigureApplicationCookie 的代码块,已经进行了测试。虽然我没看到应用程序行为上的任何变化,但这只相当于简单的注销和重新登录。更多信息 - R. Richards
1
我收到了 OptionsValidationException: Cookie.Expiration被忽略,请使用ExpireTimeSpan。 - Mugen
显示剩余2条评论

1

这段代码对我有效。只有第二个块更改了Cookie的过期时间。

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.Cookie.SameSite = SameSiteMode.Strict;
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
            options.LoginPath = "/Account/Login";
            options.LogoutPath = "/Account/Logout";
            options.AccessDeniedPath = "/Account/AccessDenied";
        });

        services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings, only this changes expiration
            options.Cookie.HttpOnly = true;
            options.Cookie.Expiration = TimeSpan.FromDays(150);
            options.ExpireTimeSpan = TimeSpan.FromDays(150);
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接