【问题标题】:How to fetch authenticated username in web api (token based authentication)如何在 Web api 中获取经过身份验证的用户名(基于令牌的身份验证)
【发布时间】:2017-05-30 22:37:26
【问题描述】:

我已经实现了基于令牌的 web api 身份验证。

现在,在 web api 端,我需要获取经过身份验证的用户名,但无法获取。

当我尝试使用RequestContext.Principal.Identity.Name 访问时,它会显示null 值。

在最坏的情况下,我需要在每个请求上从 angualrjs 传递用户名。如果它从 web api 本身检索,那将是最好的。

谁能帮忙,我如何从 web api 端获取经过身份验证的用户。

谢谢

【问题讨论】:

    标签: angularjs authentication asp.net-web-api asp.net-web-api2 token


    【解决方案1】:

    你需要设置用户名才能认领。

     var claim = ((ClaimsIdentity)identity).FindFirst("sub");
        string username = claim.Value
    

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2016-07-31
      • 2016-11-30
      • 2019-03-12
      • 2014-03-26
      相关资源
      最近更新 更多