首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >.NET核心2 CookieAuthentication忽略过期时间跨度

.NET核心2 CookieAuthentication忽略过期时间跨度
EN

Stack Overflow用户
提问于 2018-06-04 04:24:41
回答 3查看 11.2K关注 0票数 7

我正在使用CookieAuthentication开发一个.NET Core2.1Web应用程序。由于某些原因,在CookieAuthenticationOptions对象上设置ExpireTimeSpanCookie.Expiration对Cookie的生存期没有影响。Chrome总是显示与1969-12-31T23:59:59.000Z相同的过期日期。因此,在关闭浏览器窗口后,cookie就消失了。

Startup.cs

代码语言:javascript
复制
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?}");
   });
}

在SignIn上的我正在使用这个代码

代码语言:javascript
复制
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.AddAuthentication之后尝试过services.ConfigureApplicationCookie,就像这个答案中的Cookie expiry in ASP.NET Core 2.0 with Identity

我遗漏了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-04 05:00:28

Use IsPersistent = true

示例

代码语言:javascript
复制
var claims = new List<Claim>
{
    new Claim(ClaimTypes.NameIdentifier, client.Id),
    new Claim(ClaimTypes.Role, client.Role)
};

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

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
          new ClaimsPrincipal(identity),
          new AuthenticationProperties
          {
              ExpiresUtc = DateTime.UtcNow.AddYears(1),
              IsPersistent = true
          });
票数 4
EN

Stack Overflow用户

发布于 2019-03-14 06:29:51

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

代码语言:javascript
复制
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核心3.1 (不再需要cooke.expiration作为单独选项)的更新:

代码语言:javascript
复制
services.AddAuthentication();

services.ConfigureApplicationCookie(options =>
    {
        options.ExpireTimeSpan = TimeSpan.FromDays(14);
        options.SlidingExpiration = false;
   });
 
services.AddMvc();
票数 5
EN

Stack Overflow用户

发布于 2020-09-02 19:52:45

身份具有专用cookie配置选项CookieAuthenticationOptions和cookie过期值已决定忽略,可以在此处找到一些说明:Github issue Test reference

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50670654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档