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

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

问题描述

使用asp.net身份1.0.0-RC1实体框架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).

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

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

在过去窗体身份验证我用下面的code,以解决这个问题。

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的身份做到及时更新饼干?

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

更新

以下code似乎更新验证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});

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

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

推荐答案

<一个href=\"http://stackoverflow.com/questions/19091157/how-do-you-login-authenticate-a-user-with-asp-net-mvc5-rtm-bits-using-aspnet-ide?rq=1\">How你登录/认证使用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,您可以使用类似code。

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。另外,还要确保同时发出登入票,您正在设置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;
}

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

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