问题
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