Asp.net mvc 5 如何在使用asp.net标识更改当前用户的用户名后更改身份验证Cookie

Asp.net mvc 5 如何在使用asp.net标识更改当前用户的用户名后更改身份验证Cookie,asp.net-mvc-5,owin,asp.net-identity,Asp.net Mvc 5,Owin,Asp.net Identity,使用asp.net identity版本1.0.0-rc1和实体框架6.0.0-rc1(Visual Studio 2013 RC附带的那些) 尝试为用户提供更改其用户名的机会。 在AuthenticationIdentityManager下似乎没有该功能,因此我使用EF(获取当前用户的用户对象,更改用户名并保存更改)更改数据 问题是身份验证cookie保持不变,下一个请求失败,因为没有这样的用户 在过去的表单身份验证中,我使用以下代码来解决这个问题 var formsAuthCookie =

使用asp.net identity版本1.0.0-rc1和实体框架6.0.0-rc1(Visual Studio 2013 RC附带的那些)

尝试为用户提供更改其
用户名的机会。
在
AuthenticationIdentityManager
下似乎没有该功能,因此我使用EF(获取当前用户的用户对象,更改用户名并保存更改)更改数据

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

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

var formsAuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
var isPersistent = FormsAuthentication.Decrypt(formsAuthCookie.Value).IsPersistent;
FormsAuthentication.SetAuthCookie(newUserName, isPersistent);
我应该如何使用asp.net标识来更新Cookie

更新

下面的代码似乎更新了身份验证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
值?

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

AuthenticationManager.SignOut();
IdentityManager.Authentication.SignIn(AuthenticationManager, user.UserId, isPersistent:false);
对于持久值,您需要访问身份验证cookie并检索状态

更新:

使用适当的AuthenticationType来代替“载体”。另外,请确保在颁发登录票证时,您正在设置AuthenticationProperties.IsPersistent

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

此代码可能适用于RTM(尚未广泛提供)。在RC1中没有DefaultAuthenticationTypes和UserManager.CreateIdentityAsync()。第二个问题:如何获得当前的IsPersistent值(我的目标只是更改用户名,而不是其他任何内容)?在VS2013发布之前,最好保持夜间构建的试用。开发人员在这里的评论中很少提到许多RC1类在将于11月随VS2013发布的RTM中不可用。我已升级到夜间版本,但问题仍然存在:如何获取当前的IsPersistent值?您上次的更新帮助很大(将“Bearer”更改为DefaultAuthenticationTypes.ExternalCookie)。标记为答案的。
bool isPersistent=false;
var authContext = await Authentication.AuthenticateAsync("Bearer");
if (authContext != null)
{
   var aProperties = authContext.Properties;
   isPersistent = aProperties.IsPersistent;
}