【问题标题】:How to change Identity value in Web Api 2如何在 Web Api 2 中更改身份值
【发布时间】: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 中的身份值吗?

【问题讨论】:

    标签: c# .net oauth


    【解决方案1】:

    我想减少拥有“ChangeSessionCustomer”的复杂性,我只需重新授权用户并分配一个新令牌。

    【讨论】:

    • 这正是我想要的。 Tnkz。
    猜你喜欢
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2017-03-04
    • 1970-01-01
    • 2017-07-27
    • 2016-04-05
    相关资源
    最近更新 更多