【问题标题】:Get current user name in IdentityServer4 & ASP.net Core在 IdentityServer4 & ASP.net Core 中获取当前用户名
【发布时间】:2017-04-10 03:01:56
【问题描述】:

我按照网站上的示例进行操作,并且能够获得我的不记名令牌。

但是,如何在我的 api 控制器中获取当前用户名?我使用了 System.Security.Claims.ClaimsPrincipal 但名称为空。

谢谢。

【问题讨论】:

  • 检查索赔集合。

标签: asp.net-core-mvc identityserver4


【解决方案1】:
HttpContext.User.FindFirst("email")?.Value

并将“电子邮件”替换为名称。如果未在令牌中设置 Name 声明,则 Name 声明也将为空。您还可以在 IProfileService 的实现中将其添加到令牌中。

claims.Add(new Claim(JwtClaimTypes.Name, user.UserName));

user 是您的实体或 wharever。

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2016-08-07
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多