Get current user name in IdentityServer4 & ASP.net Core

房东的猫 提交于 2019-12-25 05:51:13

问题


I followed the example on the website and I am able to get my bearer token.

However, how do I get the current user name in my api controller? I used System.Security.Claims.ClaimsPrincipal but name is null.

Thanks.


回答1:


HttpContext.User.FindFirst("email")?.Value

And replace the "email" with name. If the Name claim is not set in the token, the Name claim will also be null. You could also add this to the token in the implementation of the IProfileService.

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

where user is your entity or wharever.



来源:https://stackoverflow.com/questions/41356186/get-current-user-name-in-identityserver4-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!