简体   繁体   English

使用 Microsoft Graph 生成不记名令牌

[英]Generate Bearer Token using Microsoft Graph

I have Web API built in .net framework 4.6.我有 Web API 内置在 .net 框架 4.6 中。 I secure my API using Azure AD.我使用 Azure AD 保护我的 API。 For the purpose of development, I need to generate token so I can use it for testing and debugging.出于开发的目的,我需要生成令牌,以便我可以使用它来进行测试和调试。 How can I generate token from Microsoft Graph that I can use to authenticate to my API?如何从 Microsoft Graph 生成可用于对我的 API 进行身份验证的令牌?

I tried this https://login.microsoftonline.com/{tenantId}/oauth2/token endpoint but the token it generates is not valid.我尝试了这个https://login.microsoftonline.com/{tenantId}/oauth2/token端点,但它生成的令牌无效。 I get 401 using the token from that endpoint.我使用来自该端点的令牌获得 401。

enter image description here在此处输入图像描述

The error "401 unauthorized" usually occurs when you missed giving resource parameter while generating access token. “401 unauthorized”错误通常发生在您在生成访问令牌时错过了提供resource parameter的情况下。

If that's the case, you will still get the access token but when you are using the token to authenticate to your API, you will get "Invalid token" error.如果是这种情况,您仍将获得访问令牌,但是当您使用令牌对 API 进行身份验证时,您将收到“无效令牌”错误。

To resolve the error, please include below parameters while generating access token:要解决该错误,请在生成访问令牌时包含以下参数:

  • Make sure to include resource parameter and other required parameters like below:确保包含resource parameter和其他必需参数,如下所示:

在此处输入图像描述

  • I tried in my environment , after including the above parameters, I got the access token successfully like below:我在我的环境中尝试过,在包含上述参数后,我成功获得了访问令牌,如下所示:

图片2

If the above solution does not work, try with different grant_type parameter.如果上述解决方案不起作用,请尝试使用不同grant_type参数。

For more information, please refer below links:有关更多信息,请参阅以下链接:

401 Unauthorized Error–Azure Active Directory (AD) – Microsoft Azure Articles.. (wordpress.com) . 401 未经授权的错误 – Azure Active Directory (AD) – Microsoft Azure 文章.. (wordpress.com)

Getting Access Token for Microsoft Graph Using OAuth REST API - DZone Security 使用 OAuth REST API 获取 Microsoft Graph 的访问令牌 - DZone Security

Azure registered app error: The user or administrator has not consented to use the application with ID - Stack Overflow Azure注册应用程序错误:用户或管理员未同意使用具有ID的应用程序 - Thinbug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM