c# - 从 Azure Active Directory 访问具有授权类型的用户
问题描述
我是 Azure 的新手。我需要从 Azure 活动目录中检索用户。我有委托类型、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 的新手,请通过一个简单的控制台应用程序代码示例帮助您轻松实现。
推荐阅读
- laravel - laravel 购物车在数据库中产品信息发生变化时更新
- google-apis-explorer - 如何使用 Google Civic API 按位置进行选举?
- java - 来自用户的单个输入行上的扫描仪输入验证
- json - JSON 值无法转换为 System.Boolean。路径:$
- snmp - 使用 SharpSnmp 时,当 V1 中的响应具有尾随数据字节时,SNMP GET snmpexception
- swift - SQLite 处理来自数据库 SWIFT 的实数
- github - GitHub Actions 未读取机密信息
- xcode - 命令 PhaseScriptExecution 失败,退出代码为非零 Xcode
- reactjs - 我如何处理 fetch api react 中的错误?
- google-cloud-platform - OAuth 同意屏幕验证 - 符合域验证要求