如何使用AzureADGraph或Microsoft Graph在AZURE AD B2C中为用户生成访问令牌?

问题描述 投票:0回答:1

有没有办法使用AZURE AD GRAPH客户端或MICROSOFT Graph客户端生成用户的访问令牌?我有用户名和密码,客户端ID,策略名称。使用所有这些参数。我想生成令牌。

谢谢!

azure azure-active-directory microsoft-graph azure-ad-b2c azure-ad-graph-api
1个回答
2
投票

我们可以这样做,但不建议使用用户名和密码来做到这一点。

一般情况下,Microsoft不建议客户使用它,因为它不如其他流程安全,并且它与条件访问不兼容(如果资源需要条件访问,则对AcquireTokenSilent的调用将失败,因为这不是交互式的在流程中,STS没有机会向用户呈现对话以告诉他/她他/她需要进行多因素认证。

演示代码。

var graphResourceId = "https://graph.windows.net";
var clientId = "afa0b3fxxxxx";
var userName= "xxxxx";
var password = "xxx";
var result = await authenticationContext.AcquireTokenAsync(graphResourceId, clientId, new UserPasswordCredential(userName, password));
var accessToken = result.AccessToken

有关更多信息,请参阅此document

更新:

获取刷新令牌。

网址:

post https://login.microsoftonline.com/{tenantId}/oauth2/token

标题:

Content-Type: application/x-www-form-urlencoded

身体

resource=https%3A%2F%2Fgraph.windows.net&client_id=xxxxx&grant_type=password&username=tom%40xxxx.onmicrosoft.com&password=xxxxx&scope=openid 

测试结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.