C# 如何在ASP.NET核心API中查看Azure AD生成的访问令牌?
如何查看ASP.NET Core 5.0 Web Api中Azure AD生成的访问令牌 有关更多上下文: 我有一个Angular客户端,它在使用MSAL V2登录后获得访问令牌。客户端可以使用获取的访问令牌调用API。如何拦截令牌?我想从中获取用户名或/和电子邮件地址C# 如何在ASP.NET核心API中查看Azure AD生成的访问令牌?,c#,asp.net-core,azure-active-directory,C#,Asp.net Core,Azure Active Directory,如何查看ASP.NET Core 5.0 Web Api中Azure AD生成的访问令牌 有关更多上下文: 我有一个Angular客户端,它在使用MSAL V2登录后获得访问令牌。客户端可以使用获取的访问令牌调用API。如何拦截令牌?我想从中获取用户名或/和电子邮件地址 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(Configuration.G
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAD"));
获取用户信息非常简单
在每个控制器中,您都可以访问具有属性的对象。您可以检查用户声明以查找其姓名和其他有关用户的信息。使用以下代码从用户声明中查找令牌:
var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
IEnumerable<Claim> claims = identity.Claims;
// or
identity.FindFirst("ClaimName").Value;
}
var identity=HttpContext.User.identity作为ClaimsIdentity;
如果(标识!=null)
{
IEnumerable claims=identity.claims;
//或
identity.FindFirst(“ClaimName”).Value;
}
有关更多信息,请参阅以下文章以了解更多信息:
要查看原始访问令牌还是查找令牌中嵌入的用户信息?最好只获取用户。我想在第一次登录时保存用户信息。谢谢,这正是我所需要的!