簡體   English   中英

如何在Web Api 2中更改身份值

[英]How to change Identity value in Web Api 2

我正在使用基於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中的標識值?

我想降低擁有“ ChangeSessionCustomer”的復雜性,我只是重新授權用戶並分配一個新令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM