如何在使用asp.net标识更改当前用户的UserName后更改身份验证cookie

ale*_*ush 18 owin asp.net-mvc-5 asp.net-identity

将asp.net身份版本1.0.0-rc1与Entity Framework 6.0.0-rc1(Visual Studio 2013 RC附带的那些)一起使用.

试图让用户有机会改变他们的UserName.似乎没有功能AuthenticationIdentityManager,所以我使用EF更改数据(获取当前用户的User对象,更改UserName并保存更改).

问题是认证cookie保持不变,下一个请求失败,因为没有这样的用户.

在过去的表单身份验证中,我使用以下代码来解决此问题.

var formsAuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
var isPersistent = FormsAuthentication.Decrypt(formsAuthCookie.Value).IsPersistent;
FormsAuthentication.SetAuthCookie(newUserName, isPersistent);
Run Code Online (Sandbox Code Playgroud)

我该怎么做asp.net身份更新cookie?

UPDATE

以下代码似乎更新了身份验证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});
Run Code Online (Sandbox Code Playgroud)

剩下的问题是:如何IsPersistent从当前身份验证cookie中提取值?

jd4*_*d4u 16

如何使用AspNet.Identity使用Asp.Net MVC5 RTM位登录/验证用户?

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
Run Code Online (Sandbox Code Playgroud)

对于RC1,您可以使用类似的代码.

AuthenticationManager.SignOut();
IdentityManager.Authentication.SignIn(AuthenticationManager, user.UserId, isPersistent:false);
Run Code Online (Sandbox Code Playgroud)

对于持久值,您需要访问身份验证cookie并检索状态.

更新:

使用适当的AuthenticationType代替"Bearer".还要确保在发出登录票证时设置AuthenticationProperties.IsPersistent.

bool isPersistent=false;
var authContext = await Authentication.AuthenticateAsync("Bearer");
if (authContext != null)
{
   var aProperties = authContext.Properties;
   isPersistent = aProperties.IsPersistent;
}
Run Code Online (Sandbox Code Playgroud)