简体   繁体   English

如何在 ASP.NET Core API 中查看 Azure AD 生成的访问令牌?

[英]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.

相关问题 如何验证 Azure AD v2 在 ASP.NET 核心 WEB ZDB97Z42387108CA81463 中生成的 OpenID Connect 访问令牌 - How to Validate OpenID Connect Access Token generated by Azure AD v2 in ASP.NET core WEB API? 如何在ASP.NET Core中为Azure AD请求正确的访问令牌以访问Microsoft Graph - How do request a correct access token in ASP.NET Core for Azure AD to access Microsoft Graph ASP.NET Core 2.0 Web API Azure Ad v2令牌授权无效 - ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working Azure AD 限制整个 ASP.NET 内核 API - Azure AD Restricts entire ASP.NET Core API 如何将Daemon或Server的Azure AD OAuth2访问令牌和刷新令牌转换为C#ASP.NET Web API - How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API 在 Azure 中部署 ASP.net 核心应用程序时,Azure AD 的 400 id 令牌无效 - 400 invalid id token for Azure AD when ASP.net core app is deployed in Azure 如何在ASP.NET WEB API中验证由identityserver4生成的OpenID Connect访问令牌 - How to Validate OpenID Connect Access Token generated by identityserver4 in ASP.NET WEB API 有没有办法使用 Azure AD 令牌对 ASP.NET Core MVC controller 操作进行身份验证? - Is there a way to authenticate an ASP.NET Core MVC controller action using an Azure AD token? ASP.NET OAuth:如何生成访问令牌? - ASP.NET OAuth: How is access token generated? 如何使用ASP.NET Core中的访问令牌向Twitter api发送请求? - How to send request to Twitter api using access token in ASP.NET core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM