Get current user name in IdentityServer4 & ASP.net Core

4.2k views Asked by At

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

There are 1 answers

0
Damienbod On
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.