[英]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?如何在ASP.NET Core 5.0 Web Z72660438ZF?C72669043DC0959F3B60C中查看Azure AD生成的访问令牌
For more context:有关更多上下文:
I have an Angular client which is getting an access token after logging in with MSAL V2.我有一个 Angular 客户端,它在使用 MSAL V2 登录后获取访问令牌。 The client can call the API with the acquired access token.
客户端可以使用获取的访问令牌调用 API。 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.在每个 controller 中,您都可以访问
HttpContext
object 并且具有User
属性。 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欲了解更多信息,您可以关注以下文章以获得更多了解: https://visualstudiomagazine.com/articles/2019/11/01/authorization-claims.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.