c# - 尝试使用 Microsoft.Graph 检索 AD 组的所有成员时出现授权错误
问题描述
我正在使用 Microsoft Graph 检索组的所有成员。但我收到以下授权错误消息。我无法弄清楚,因为我从今天开始学习它。我也浏览了一些关于此的博客,但找不到根本原因。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:Microsoft.Graph.ServiceException:代码:Authorization_RequestDenied 消息:权限不足,无法完成操作。内部错误:AdditionalData:日期:2021-09-24T11:25:11 请求 ID:ef3e82a6-f1df-4018-bccb-2eea075a934f 客户端请求 ID:ef3e82a6-f1df-4018-bccb-2eea075a934f 客户端请求 ID:ef3e82a6-f1df- 4018-bccb-2eea075a934f
以下是我的代码:
private static string appId = ConfigurationManager.AppSettings["ida:AppId"];
private static string appSecret = ConfigurationManager.AppSettings["ida:AppSecret"];
private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
private static List<string> graphScopes =
new List<string>(ConfigurationManager.AppSettings["ida:AppScopes"].Split(' '));
public static async Task<IEnumerable<Event>> GetEventsAsync()
{
var graphClient = GetAuthenticatedClient();
var members = await graphClient.Groups["00000000-0000-0000-0000-000000000000"].Members.Request().GetAsync();
}
private static GraphServiceClient GetAuthenticatedClient()
{
return new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
var idClient = ConfidentialClientApplicationBuilder.Create(appId)
.WithRedirectUri(redirectUri)
.WithClientSecret(appSecret)
.Build();
var tokenStore = new SessionTokenStore(idClient.UserTokenCache,
HttpContext.Current, ClaimsPrincipal.Current);
var accounts = await idClient.GetAccountsAsync();
var result = await idClient.AcquireTokenSilent(graphScopes, accounts.FirstOrDefault())
.ExecuteAsync();
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
}));
}
应用设置:
<appSettings>
<add key="ida:AppID" value=[App Id] />
<add key="ida:AppSecret" value=[App Secret] />
<add key="ida:RedirectUri" value="https://localhost:44359/" />
<add key="ida:AppScopes" value="User.Read.All Calendars.Read" />
</appSettings>
我假设我可能需要调整 appSettings 中的 AppScopes 值,但不确定。谁能给我一些解决这个问题的方法?
提前致谢。
解决方案
推荐阅读
- django - 由于静态文件收集原因,Heroku 拒绝推送
- excel - 我需要 vba 代码来定位形状质心的坐标
- python - 在不影响 ElementTree 的情况下将重音字符转换为拉丁语
- javascript - 从对象函数访问对象属性
- c# - '4' 和 '4' 在主键中冲突,但在文件系统中没有冲突
- postsharp - 不同系统上的异常消息不同
- testing - Allures 测试显示历史趋势并发送邮件
- scenekit - Metal 2:MetalKit (MTKView) 和 SceneKit 互操作性
- flutter - 颤振:第 33 行:sed:找不到命令
- javascript - 通过类名更改按钮背景