简体   繁体   中英

How to view access token generated by Azure AD in ASP.NET Core API?

How to view the access token generated by Azure AD in ASP.NET Core 5.0 Web Api?

For more context:

I have an Angular client which is getting an access token after logging in with MSAL V2. The client can call the API with the acquired access token. How can I intercept the token? I want to get the username or/and e-mail address from it.

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAD"));

Getting the user information is quite straight forward.

In every controller, you get access to HttpContext object and that has a User property. You can check user's claims to find their name and other information about the user.

Use the below code to find the token from the user claim:

var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
  IEnumerable<Claim> claims = identity.Claims; 
  // or
  identity.FindFirst("ClaimName").Value;

}

For more information you can follow the below article for more understanding: https://visualstudiomagazine.com/articles/2019/11/01/authorization-claims.aspx

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