更新用户声称不发生作用。为什么? [英] Update User Claim not Taking Effect. Why?

查看:190
本文介绍了更新用户声称不发生作用。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 5.1 Owin和声明身份验证。

I am using ASP.NET MVC 5.1 with Owin and Claims authentication.

用户更改其电子邮件,我需要更新用户的请求后,让我在控制器尝试:

After the user changes its email I need to update the users claims, so I tried in the controller:

  ClaimsIdentity identity = (ClaimsIdentity)User.Identity;
  Claim claim = identity.FindFirst(ClaimTypes.Email);
  identity.RemoveClaim(claim);
  identity.AddClaim(new Claim(ClaimTypes.Email, newEmail));

  IOwinContext context = new OwinContext();

  context.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
  context.Authentication.SignIn(identity);

索赔的是改变了,但是当我刷新页面的电子邮件号称是原装再次...

The Claim is changed but when I refresh the page the email claims is the original again ...

看来cookie不会被更新。任何知道我做错了?

It seems the cookie is not being updated. Any idea what I am doing wrong?

和是否有可能获得身份IsPersistent的值,所以当我再次登录这我有同样的价值?

And is it possible to get the value of "IsPersistent" from the identity so when I sign it again I will have the same value?

感谢你,

米格尔

推荐答案

我有这个同样的问题,所以才想在这里总结一下我的发现。克里斯说,答案的基础的确是在这里:<一href=\"http://stackoverflow.com/questions/19349011/how-to-change-authentication-cookies-after-changing-username-of-current-user-wit\">How改变身份验证Cookie改变与asp.net的身份当前用户的用户名后但我发现线程有点难以遵循,而这个问题是不是一个真正的直接的重复。

I had this same problem, so just wanted to summarise my findings here. As Chris says, the basis of the answer is indeed here: How to change authentication cookies after changing UserName of current user with asp.net identity but I found that thread a bit hard to follow, and that question isn't really a direct duplicate.

首先,获得<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.owin.security.iauthenticationmanager%28v=vs.111%29.aspx\">AuthenticationManager从当前OWIN上下文。一旦你有,你就可以得到isPersistent(从原来的签到通话和其他属性)的值,通过调用 AuthenticateAsync 方法。然后更新您只需要更换这样的 AuthenticationResponseGrant 属性的值当前用户身份的主张:

To begin, get the AuthenticationManager from the current OWIN context. Once you have that, you can get the value of "isPersistent" (and other properties from the original SignIn call), by calling the AuthenticateAsync method. Then to update the claims of the current user identity you just need to replace the value of the AuthenticationResponseGrant property like this:

var identity = (ClaimsIdentity)User.Identity;

// Call AddClaim, AddClaims or RemoveClaim on the user identity.

IOwinContext context = Request.GetOwinContext();

var authenticationContext = 
    await context.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);

if (authenticationContext != null)
{
    authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
        identity,
        authenticationContext.Properties);
}

这是实际更新cookie中的 AuthenticationResponseGrant 财产的最后设置。

希望这有助于其他读者。

Hope this helps other readers.

这篇关于更新用户声称不发生作用。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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