使用asp.net身份更改当前用户的用户名后如何更改身份验证cookie [英] How to change authentication cookies after changing UserName of current user with asp.net identity

查看:25
本文介绍了使用asp.net身份更改当前用户的用户名后如何更改身份验证cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using asp.net identity version 1.0.0-rc1 with Entity Framework 6.0.0-rc1 (the ones that come with Visual Studio 2013 RC).

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

Trying to give users an opportunity to change their UserName. There seems to be no function for that under AuthenticationIdentityManager, so I change the data using EF (get User object for current user, change UserName and save changes).

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

The problem is that authentication cookies remain unchanged, and the next request fails as there is no such user.

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

With forms authentication in the past I used the following code to solve this.

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

我应该如何使用 asp.net 身份来更新 cookie?

What should I do with asp.net identity to update the cookies?

更新

以下代码似乎更新了身份验证 cookie.

The following code seems to update the authentication 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值?

The remaining problem is: how to extract IsPersistent value from current authentication cookie?

推荐答案

如何使用 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);
}

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

For the RC1, You can use the similar code.

AuthenticationManager.SignOut();
IdentityManager.Authentication.SignIn(AuthenticationManager, user.UserId, isPersistent:false);

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

For persistent value, you need to access the authentication cookie and retrieve the status.

更新:

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

Use appropriate AuthenticationType used in place of "Bearer". Also make sure while issuing the signin ticket, you are setting the AuthenticationProperties.IsPersistent.

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

这篇关于使用asp.net身份更改当前用户的用户名后如何更改身份验证cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆