我目前正在尝试将用户的电子邮件/用户名从移动应用程序更新为Web API项目.我目前正在使用oauth和令牌身份验证.更新身份用户时,用户将变为未经身份验证,因为用户名和访问令牌不再有效.根据我的阅读,我必须更新身份声明.这是我到目前为止所尝试的:
var identity = new ClaimsIdentity(User.Identity);
if (result)
{
var identityUser = await UserManager.FindByNameAsync(User.Identity.Name);
identityUser.Email = AntiXssEncoder.HtmlEncode(value.Email, true);
identityUser.UserName = AntiXssEncoder.HtmlEncode(value.Email, true);
var identityResult = await UserManager.UpdateAsync(identityUser);
if(identityResult.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
await UserManager.RemoveClaimAsync(identityUser.Id, identity.FindFirst(ClaimTypes.Name));
await UserManager.AddClaimAsync(identityUser.Id, new Claim(ClaimTypes.Name, value.Email));
identity.RemoveClaim(identity.FindFirst(ClaimTypes.Name));
identity.AddClaim(new Claim(ClaimTypes.Name, value.Email));
authenticationManager.AuthenticationResponseGrant =
new AuthenticationResponseGrant(
new ClaimsPrincipal(identity),
new AuthenticationProperties { IsPersistent = false });
}
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
但是,它仍然显示使用时的上一封电子邮件,User.Identity.Name并且其中的用户声明authenticationManager尚未更新.我不知道还能做什么,因为Web API没有太多关于此的文档.任何帮助是极大的赞赏.