gpt4 book ai didi

c# - Microsoft 图形 API 如何创建不记名 msal token

转载 作者:行者123 更新时间:2023-11-30 23:09:33 25 4
gpt4 key购买 nike

尝试作为来自基本控制台应用程序的应用程序(而不是作为用户)进行身份验证,并获取具有正确范围的访问 token 。我设法获得了一个 token ,但没有正确的范围。我不确定您是否能够这样做,我错过了什么?

static async Task<string> GetMsalToken()
{
const string clientId = "xxxx234a3-7b9f-42341c-86df-3d2349861";
const string clientSecret = "WX324XXXXXXXXXXXXX";
var scopes = new List<string>() { "https://graph.microsoft.com/.default" };

var clientCredential = new Microsoft.Identity.Client.ClientCredential(clientSecret);
var redirecturi = "msalxxxx234a3-7b9f-42341c-86df-3d2349861://auth";

var clientApplication = new Microsoft.Identity.Client.ConfidentialClientApplication(clientId, redirecturi, clientCredential, null, null);
var authenticationResult = await clientApplication.AcquireTokenForClientAsync(scopes);

return authenticationResult.AccessToken;
}

最佳答案

您在 Azure AD V2.0 终结点中使用应用程序权限使用客户端凭据身份验证,此请求中为范围参数传递的值应为您想要的资源的资源标识符(应用程序 ID URI),后缀为 .默认后缀。例如Microsoft Graph,值为https://graph.microsoft.com/.default .

使用客户端凭证流,应用程序使用所有组织的数据,而不是特定用户的数据。在这种情况下,在 Azure AD 中使用“管理员同意”(admin consent),并且这种同意必须由组织中的管理员完成。当您使用管理员同意时,您所要做的就是转到 https://login.microsoftonline.com/{tenant name}/adminconsent?client_id={application id}&state={some state data}&redirect_uri ={redirect uri}使用网络浏览器。

在管理员同意后,如果您使用 online tool 解码您的访问 token ,则使用客户端凭证流获取 Microsoft Graph 的 token 。 ,您会发现应用程序权限列在 roles claim 中。请确认您已完成应用程序权限的管理员同意。

Here这是有关将客户端凭据流与 Azure AD V2.0 端点结合使用的教程。

关于c# - Microsoft 图形 API 如何创建不记名 msal token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45758207/

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