【发布时间】:2017-10-19 16:46:07
【问题描述】:
我正在制作一个使用 Identity 的 ASP.NET MVC 5 应用程序。身份验证的一部分包括将声明存储在 cookie 中,而不将其保存到数据库中,因为该声明只能持续到用户注销。此声明在注销后添加到用户身份中,并且用户可以在登录时随时更改声明的值。为此,我使用以下代码:
var AuthenticationManager = HttpContext.GetOwinContext().Authentication;
var Identity = User.Identity as ClaimsIdentity;
if (Identity.HasClaim(c => c.Type == "custom"))
{
Identity.RemoveClaim(Identity.FindFirst("custom"));
}
Identity.AddClaim(new Claim("custom", "value", ClaimValueTypes.Integer32));
AuthenticationManager.AuthenticationResponseGrant =
new AuthenticationResponseGrant(new ClaimsPrincipal(Identity), new AuthenticationProperties { IsPersistent = true });
这在一段时间内可以正常工作......但在用户登录后大约十分钟,声明就消失了!如何使声明持续到注销?
【问题讨论】:
-
存储在 cookie 中可能不是一个好主意
标签: c# cookies asp.net-mvc-5 asp.net-identity-2