IsPersistent 在 OWIN Cookie 身份验证中的工作原理
How IsPersistent works in OWIN Cookie authentication
好像不太明白IsPersistent
在OWIN cookie认证中是如何工作的,下面的代码是使用IsPersistent
:
var context = Request.GetOwinContext();
var authManager = context.Authentication;
var properties = new AuthenticationProperties { IsPersistent = isPersistence };
authManager.SignIn(properties, identity);
当用户 checks/unchecks Remember me
(后面使用 IsPersistent
)时我没有看到区别,因为如果我关闭 Chrome 浏览器并再次打开它网站,cookie .AspNet.ApplicationCookie
仍然存在,即使我选中或取消选中 Remember me
.
,它也能让我进入
我检查了 link 上 IsPersistent
的定义:
Gets or sets whether the authentication session is persisted across multiple requests.
但由于我看到它仍然有效,所以不太了解。
设置 OWIN cookie 身份验证的代码:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = ApplicationTypes.ApplicationCookie,
ExpireTimeSpan = TimeSpan.FromMinutes(30),
LoginPath = new PathString("/Account/LogOn")
});
持久性 cookie 将作为文件保存在浏览器文件夹中,直到它们过期或被手动删除。即使您关闭浏览器,这也会导致 cookie 持续存在。
如果IsPersistent 设置为false,浏览器将获取会话cookie,当浏览器关闭时该cookie 将被清除。
现在重新启动浏览器后会话 cookie 无法清除的原因是 chrome 默认设置。
要修复它,请转到 chrome settings -> advanced,然后取消选中 Continue 运行 background apps当 Google Chrome 在 System 部分下关闭 时。
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;
}
.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)
});
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 库的源代码,我发现我们只需要使用提供程序启用 IsPersistent 标志,其他属性我们需要在默认设置中更改。
此示例适用于 4.2.0 版本的 OWIN 库。 (ASP.NET MVC5 .NET Framework 4.8)
好像不太明白IsPersistent
在OWIN cookie认证中是如何工作的,下面的代码是使用IsPersistent
:
var context = Request.GetOwinContext();
var authManager = context.Authentication;
var properties = new AuthenticationProperties { IsPersistent = isPersistence };
authManager.SignIn(properties, identity);
当用户 checks/unchecks Remember me
(后面使用 IsPersistent
)时我没有看到区别,因为如果我关闭 Chrome 浏览器并再次打开它网站,cookie .AspNet.ApplicationCookie
仍然存在,即使我选中或取消选中 Remember me
.
我检查了 link 上 IsPersistent
的定义:
Gets or sets whether the authentication session is persisted across multiple requests.
但由于我看到它仍然有效,所以不太了解。
设置 OWIN cookie 身份验证的代码:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = ApplicationTypes.ApplicationCookie,
ExpireTimeSpan = TimeSpan.FromMinutes(30),
LoginPath = new PathString("/Account/LogOn")
});
持久性 cookie 将作为文件保存在浏览器文件夹中,直到它们过期或被手动删除。即使您关闭浏览器,这也会导致 cookie 持续存在。
如果IsPersistent 设置为false,浏览器将获取会话cookie,当浏览器关闭时该cookie 将被清除。
现在重新启动浏览器后会话 cookie 无法清除的原因是 chrome 默认设置。 要修复它,请转到 chrome settings -> advanced,然后取消选中 Continue 运行 background apps当 Google Chrome 在 System 部分下关闭 时。
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;
}
.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)
});
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 库的源代码,我发现我们只需要使用提供程序启用 IsPersistent 标志,其他属性我们需要在默认设置中更改。
此示例适用于 4.2.0 版本的 OWIN 库。 (ASP.NET MVC5 .NET Framework 4.8)