使用带有 Entity Framework 6.0.0-rc1(这是 Visual Studio 2013 RC 的一部分)的 asp.net identity 版本 1.0.0-rc1。
尝试为用户提供更改他们的UserName
的机会。在AuthenticationIdentityManager
下似乎没有此功能,因此我使用 EF 更改了数据(获取当前用户的 User 对象,更改 UserName 并保存更改)。
问题在于身份验证 cookie 保持不变,因此下一次请求失败,因为没有这样的用户。
过去我使用以下代码解决了 Forms 身份验证的问题。
var formsAuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
var isPersistent = FormsAuthentication.Decrypt(formsAuthCookie.Value).IsPersistent;
FormsAuthentication.SetAuthCookie(newUserName, isPersistent);
我该如何处理asp.net身份验证以更新cookies?
更新
以下代码似乎可以更新身份验证cookie。
var identity = new ClaimsIdentity(User.Identity);
identity.RemoveClaim(identity.FindFirst(identity.NameClaimType));
identity.AddClaim(new Claim(identity.NameClaimType, newUserName));
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant
(new ClaimsPrincipal(identity), new AuthenticationProperties {IsPersistent = false});
剩下的问题是:如何从当前身份验证Cookie中提取IsPersistent
的值?