gpt4 book ai didi

c# - 如何从AAD应用程序获取ResourceId

转载 作者:行者123 更新时间:2023-12-03 05:10:48 25 4
gpt4 key购买 nike

我想使用下面的代码片段从 AAD 应用程序获取访问 token 。

var tokenCredential = new DefaultAzureCredential();
var accessToken = await tokenCredential.GetTokenAsync(
new TokenRequestContext(scopes: new string[] { ResourceId + "/.default" }) { }
);

我已经在Azure门户上创建了AAD应用程序,如何从AAD应用程序中获取ResourceId?

enter image description here

最佳答案

Note that: Resource ID depends on the Azure resource you want to authenticate the Azure AD Application (Microsoft Graph, Web Api etc).

对于示例,我将 https://graph.microsoft.com 作为 resourceId 传递以对 Microsoft 进行身份验证图形API。

using Azure.Core;
using Azure.Identity;

// Define the resource ID for the Azure AD application you want to access.
string resourceId = "https://graph.microsoft.com";

var tokenCredential = new DefaultAzureCredential();

var accessToken = await tokenCredential.GetTokenAsync(
new TokenRequestContext(scopes: new string[] { resourceId + "/.default" })
);

Console.WriteLine(accessToken.Token);

enter image description here

我同意@juunas,如果您想验证 Web Api,您可以将 resourceId 作为 Azure AD 应用程序的 ClientID 或 API URL 传递,如下所示:

enter image description here

Note that: To fetch the access token for web Api, you must add Microsoft Azure CLI with client ID04b07795-8ddb-461a-bbee-02f9e1bf7b46 as the Authorized client application.

转到 Azure AD 应用 -> 公开 API -> 使用 04b07795-8ddb-461a-bbee-02f9e1bf7b46 添加客户端应用程序并检查范围。 p>

enter image description here

并确保授予 API 权限:

enter image description here

using Azure.Core;
using Azure.Identity;


// Define the resource ID for the Azure AD application you want to access.
string resourceId = "api://ClientID";

var tokenCredential = new DefaultAzureCredential();

var accessToken = await tokenCredential.GetTokenAsync(
new TokenRequestContext(scopes: new string[] { resourceId + "/.default" })
);

Console.WriteLine(accessToken.Token);

enter image description here

关于c# - 如何从AAD应用程序获取ResourceId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77049854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com