首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Security.Claims.ClaimsPrincipal未更新

System.Security.Claims.ClaimsPrincipal未更新
EN

Stack Overflow用户
提问于 2013-06-14 03:16:24
回答 1查看 8.8K关注 0票数 1

我正在使用BrockAllen.MembershipReboot

与索赔处理有关的索赔处理问题与索赔的确切更新时间有关。下面的代码应该可以演示我的问题...

代码语言:javascript
复制
private function UpdateGender(string newGender)
{
    account.RemoveClaim(ClaimTypes.Gender);
    account.AddClaim(ClaimTypes.Gender, newGender);
    userAccountService.Update(account);

    // since we've changed the claims, we need to re-issue the cookie that
    // contains the claims.
    authSvc.SignIn(User.Identity.Name);
}

[HttpPost]
public JsonResult function myAjaxMethod(){
    UpdateGender("male");

    string gender = System.Security.Claims.ClaimsPrincipal.Current.Claims.GetValue(ClaimTypes.Gender);

    // the "gender" variable will never be "male" in this request (unless it was already male)
    // because although we've set the cookie it hasn't updated the claim until the next request 
    // when it reads the cookie again.
    return Json(gender);
}

我的问题是:

有没有办法强制System.Security.Claims.ClaimsPrincipal.Current.Claims.GetValue()方法在cookie发出时更新它的声明?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-14 15:12:32

因为ClaimsPrincipal.Current在本地访问Thread.CurrentPrincipal,所以我想您可以在当前请求的生命周期内更新当前线程主体。

代码语言:javascript
复制
  // your existing code
  account.RemoveClaim(ClaimTypes.Gender);
  account.AddClaim(ClaimTypes.Gender, newGender);

  // additional code that updates current thread principal 
  ClaimsPrincipal principal = Thread.CurrentPrincipal as ClaimsPrincipal;
  if ( principal != null ) {

      ClaimsIdentity identity = principal.Identities.ElementAt(0);
      identity.AddClaim( new Claim( ClaimTypes.Gender, "asdf" ) );
  }

  // this works now
  string gender = ClaimsPrincipal.Current.Claims.GetValue( ClaimTypes.Gender );

请注意,由于您正在重新发出cookie,因此下一个请求应正确地采用您的更改。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17095246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档