Reputation: 21
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"));
Upvotes: 0
Views: 995
Reputation: 716
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
Upvotes: 1
Reputation: 136386
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.
Upvotes: 0