简体   繁体   中英

Always getting null value in User Details from Azure AD after authentication .NET Core (3.0) Web API

I am working in .NET Core 3.0 Web API and using Azure AD authentication. So I'm trying to get User Name and Email of the active logged-in User from the Claims object.

Here is what I have tried

        var cu = _httpContextAccessor.HttpContext.User;

        string username = User.FindFirstValue(ClaimTypes.Name);

When executing the username is null. This is the same when we change ClaimTypes.Email.

How can I get those details.

But I can see the value when debugging inside User.Identity.Claims . But I can't extract from there.

ClaimTypes.Name value follows the old SAML compatible format http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name while JWT tokens uses shorter versions like name .

Add the following line to your ConfigureServices method to build the claims collection using the JWT token claim names.

JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM