IsPersistent如何在OWIN Cookie身份验证中工作

cuo*_*gle 35 c# asp.net-mvc owin asp.net-mvc-5

我似乎不清楚如何IsPersistent在OWIN cookie身份验证中工作,下面的代码是使用IsPersistent:

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

authManager.SignIn(properties, identity);
Run Code Online (Sandbox Code Playgroud)

当用户检查/取消选中Remember me(IsPersistent后面使用)时,我没有看到区别,因为如果我关闭Chrome浏览器并再次打开它以与网站一起使用,则cookie .AspNet.ApplicationCookie仍然存在,即使我检查或取消选中它也可以让我进入Remember me.

我已检查链接IsPersistent上的定义:

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

但是,因为我看到它仍然有效,所以不要太了解.

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

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

Hez*_*zye 69

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

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

现在重启浏览器后会话cookie不会清除的原因是Chrome默认设置.要解决此问题,请转到Chrome 设置 - > 高级,并在系统部分关闭Google Chrome时取消选中继续运行后台应用.

  • 实际上还有另一个Chrome设置会影响浏览器是否清除Cookie.这是"继续你离开的地方"这个好回答[SO Post](http://stackoverflow.com/a/33904951/2788458)如果设置那么cookie就不会被清除 (4认同)
  • 很棒的答案,这是我第一次知道此设置,非常感谢 (2认同)

Bal*_*ran 5

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;
}
Run Code Online (Sandbox Code Playgroud)