我正在使用BrockAllen.MembershipReboot
与索赔处理有关的索赔处理问题与索赔的确切更新时间有关。下面的代码应该可以演示我的问题...
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发出时更新它的声明?
发布于 2013-06-14 15:12:32
因为ClaimsPrincipal.Current在本地访问Thread.CurrentPrincipal,所以我想您可以在当前请求的生命周期内更新当前线程主体。
// 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,因此下一个请求应正确地采用您的更改。
https://stackoverflow.com/questions/17095246
复制相似问题