【发布时间】:2015-07-26 05:35:16
【问题描述】:
我正在开发一个使用基于 OAuth 令牌进行身份验证的 Web api。
生成令牌后,我在身份上注册。
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim("CustomerId", usuario.IdCustomer.ToString()));
var principal = new GenericPrincipal(identity, new string[] { role.Profile.ToString() });
Thread.CurrentPrincipal = principal;
context.Validated(identity);
在特定时刻,我需要更改身份声明值。 我可以访问和操作,但更改不会生效。
[Authorize]
[HttpPatch]
[Route("current/customer/{id}")]
public async Task ChangeSessionCustomer(int id)
{
var context = HttpContext.Current;
IAuthenticationManager AuthenticationManager = context.GetOwinContext().Authentication;
var identity = (ClaimsIdentity)User.Identity;
var claimCustomerId = identity.FindFirst("CustomerId");
identity.RemoveClaim(claimCustomerId);
identity.AddClaim(new Claim("CustomerId", id.ToString()));
}
有可能改变 Thered.Principal 中的身份值吗?
【问题讨论】: