【问题标题】:How IsPersistent works in OWIN Cookie authenticationIsPersistent 在 OWIN Cookie 身份验证中的工作原理
【发布时间】:2015-11-03 23:06:16
【问题描述】:

我好像不太明白OWIN cookie认证中IsPersistent是如何工作的,下面的代码是使用IsPersistent

var context = Request.GetOwinContext();
var authManager = context.Authentication;
var properties = new AuthenticationProperties { IsPersistent = isPersistence };

authManager.SignIn(properties, identity);

当用户选中/取消选中 Remember me(在后面使用 IsPersistent)时,我看不到区别,因为如果我关闭 Chrome 浏览器并再次打开它以访问网站,cookie .AspNet.ApplicationCookie 仍然存在并且即使我选中或取消选中Remember me,它也能让我进入。

我已经检查了linkIsPersistent的定义:

获取或设置身份验证会话是否跨多个请求保持。

但没有得到太多理解,因为我看到它仍然有效。

设置 OWIN cookie 身份验证的代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationMode = AuthenticationMode.Active,
    AuthenticationType = ApplicationTypes.ApplicationCookie,
    ExpireTimeSpan = TimeSpan.FromMinutes(30),
    LoginPath = new PathString("/Account/LogOn")
});

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5 owin


    【解决方案1】:

    持久性 cookie 将作为文件保存在浏览器文件夹中,直到它们过期或手动删除。即使您关闭浏览器,这也会导致 cookie 持续存在。

    如果 IsPersistent 设置为 false,浏览器将获取会话 cookie,该会话 cookie 在浏览器关闭时被清除。

    现在重启浏览器后会话 cookie 无法清除的原因是 chrome 默认设置。 要修复它,请转到 chrome settings -> advanced,然后在 System 下取消选中 Google Chrome 关闭时继续运行后台应用程序 em> 部分。

    【讨论】:

    • 很棒的答案,我第一次知道这个设置,非常感谢
    • 其实还有一个Chrome设置会影响浏览器是否清除cookie。它是“从你离开的地方继续”SO Post 的好答案如果设置则 cookie 不会被清除
    • 截至目前,在 .net core 3.1 下,“继续在后台运行”对我的测试没有影响。 DID 的影响是“从你离开的地方继续”因为我想确保用户始终退出,我将以下代码放在启动时执行的初始控制器中: public async Task IndexAsync() { if ( _signInManager.IsSignedIn(HttpContext.User)) { 等待 _signInManager.SignOutAsync();返回重定向操作(); } 返回视图(); }
    • 截至 28.10.2020 MVC 5.2 asp.net 4 两个选项 Continue running ... 和 Continue where ...对 cookie 没有任何影响 - 它在选项卡/浏览器关闭之间持续存在跨度>
    【解决方案2】:
    public void Configuration(IAppBuilder app)
    {
        //Some Code
        app.UseCookieAuthentication(GetCookieAuthenticationOptions());
        //Some Code
    }
    
    private static CookieAuthenticationOptions GetCookieAuthenticationOptions()
    {
        var options  = new CookieAuthenticationOptions();
        {
            CookieName = "AuthCookie",  //Some cookie settings here
        };
        var provider = (CookieAuthenticationProvider)options.Provider;
        provider.OnResponseSignIn = (context) => 
        {
            context.Properties.IsPersistent = true;
            context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(24);
        };
        return options;
    }
    

    【讨论】:

    • 不适用于核心。核心库中没有 CookieAuthenticationProvider。
    • 拯救了我的一天。试图先用 OnValidateIdentity 来做,但它没有用。谢谢!
    【解决方案3】:

    .Net Core 2.2 的持久 cookie 你可能想试试这个

    例子

    var claimsIdentity = new ClaimsIdentity(new[]
     { new Claim(ClaimTypes.Name, "test"),},CookieAuthenticationDefaults.AuthenticationScheme);
    
    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties
    {
      IsPersistent = true,
      ExpiresUtc = DateTime.UtcNow.AddMinutes(30)
    });
    

    【讨论】:

      【解决方案4】:

      Balaji Gunasekaran,感谢您的回答。

      但我有一些修改,也许有人会使用它:

      app.UseCookieAuthentication(new CookieAuthenticationOptions
              {
                  AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
                  CookieName = "YourCookieName",
                  SlidingExpiration = true,
                  ExpireTimeSpan = TimeSpan.FromMinutes(10),
      
                  Provider = new CookieAuthenticationProvider
                  {
                      OnResponseSignIn = context =>
                      {
                          context.Properties.IsPersistent = true;
                      },
      
                  }
              });
      

      我花了一些时间学习 OWIN 库的源代码,发现我们只需要使用 provider 启用 IsPersistent 标志,其他属性我们需要在默认设置中更改。

      这个例子是实际到 4.2.0 版本的 OWIN 库。 (ASP.NET MVC5 .NET Framework 4.8)

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 2015-10-09
        • 2015-03-13
        • 2023-03-31
        • 2017-05-06
        • 1970-01-01
        • 1970-01-01
        • 2020-03-19
        • 2017-02-01
        相关资源
        最近更新 更多