azure - 如何在不注册或登录网络应用程序的情况下获取访问 token ?

标签 azure asp.net-core azure-active-directory microsoft-graph-api azure-ad-b2c

我在 Web API 上使用 azure Graph API 创建了 b2c 用户,因为我必须在服务器端而不是客户端(例如 Web 应用程序或移动应用程序)创建用户。我在链接( https://learn.microsoft.com/en-us/previous-versions/azure/ad/graph/api/users-operations#CreateLocalAccountUser )中执行了步骤。我的问题是我无法获取用于访问 protected Web API 的访问 token 。如何获取访问 token ?

最佳答案

@Md Farid Uddin Kiron 是正确的,但您也可以在没有 AD B2C 用户的情况下获得访问 token 。

https://learn.microsoft.com/en-us/graph/auth-v2-service?view=graph-rest-1.0#4-get-an-access-token

token 请求的构建如下:

POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

client_id=535fb089-9ff3-47b6-9bfb-4f1264799865
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret=qWgdYAmab0YSkuL1qKv5bPX
&grant_type=client_credentials

广告 B2C 示例:

https://login.microsoftonline.com/contosob2c.onmicrosoft.com/oauth2/v2.0/token

client_id 来自您的应用注册:

enter image description here

scope 是来自公开 API应用程序 ID URI,后跟 /.default。如果没有/.default,您将收到如下错误:

"error": "invalid_scope", "error_description": "AADSTS1002012: The provided value for scope https://contosob2c.onmicrosoft.com/11111111-1111-1111-1111-111111111111 is not valid. Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI)...", "error_codes": [ 1002012 ],

enter image description here

如果您想从 Microsoft Graph 访问某些内容,请将范围设置为https://graph.microsoft.com/.default

enter image description here

client_secret 来自证书和 secret

完整请求:

enter image description here

关于azure - 如何在不注册或登录网络应用程序的情况下获取访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56393182/

相关文章:

azure - 我可以在其他区域设置应用程序洞察吗?

azure - Azure 计算将事件推送到 Azure EventGrid 时是否很慢?有没有办法更快地获取事件?

c# - DelegateHandler 的 DI 无法正常工作

asp.net-core - 在 Azure DevOps 管道中设置连接字符串

asp.net-core - 为什么 User.FindFirstValue(claimtypes.nameidentifier) 总是返回 null?

security - Azure Active Directory v2.0 电子邮件地址验证

azure - 如何使用 MS Graph 列出给定 LiveID 帐户的 Azure AD 实例?

.net - Azure 序列化中的 DTO 问题

azure - 如何使用API​​获取azure资源的定价详细信息?

python-3.x - 将 Azure AD 中的主体 ID 解析为用户、服务