gpt4 book ai didi

c# - 从 Azure Active Directory 访问具有委派许可类型的用户

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

我是 Azure 新手。我需要从 Azure Active Directory 检索用户。我有委托(delegate)类型、user.Read 和 users.Readall。

我能够获取不记名代码,但当它检索用户时,我的权限被拒绝。我需要用 C# 编写一个控制台应用程序,下面是我尝试过的代码。

var app = ConfidentialClientApplicationBuilder.Create(clientID)
.WithClientSecret(Secret)
.WithRedirectUri(URL)
.WithAuthority("https://login.microsoftonline.com/tenantID/oauth2/token")
.Build();

string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
var token = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;

- GraphServiceClient graphClient1 = new
GraphServiceClient("https://graph.microsoft.com/v1.0", new
DelegateAuthenticationProvider(async (requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
}));

var users = graphClient1.Users.Request().Top(99).GetAsync();
var organization = graphClient1.Organization.Request().GetAsync().Result;

我正在下面

system.AggregateException: 'One or more errors occurred. (Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
for the LIne **var organization = graphClient1.Organization.Request().GetAsync().Result;**
Need help on this

提前致谢。

最佳答案

由于您使用的是委派权限,因此需要用户登录才能使用这些权限。因此,https://graph.microsoft.com/.default 不是正确的范围,它应该是委派权限,例如 https://graph.microsoft.com/User.Read.All

此外,您需要在图形请求之前使用await,

var users = await graphClient.Users.Request().Top(99).GetAsync();

由于您是 Ms Graph 的新手,请浏览一个简单的控制台应用程序 code sample帮助您轻松实现。

关于c# - 从 Azure Active Directory 访问具有委派许可类型的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63810897/

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