c# - 微软图: how to get all groups reusing the token generated through ADAL

标签 c# azure azure-active-directory microsoft-graph-api adal

我正在尝试使用 Microsoft Graph,但我有点迷失了。我正在使用 ADAL 通过 AuthenticationContext.AcquireTokenAsync 方法对我的应用程序(应用程序 ID + key )进行身份验证。代码如下所示:

AuthenticationContext AuthContext = new AuthenticationContext(Authority);
ClientCredential ClientCredential = new ClientCredential(ClientId, AppKey);
var result = await AuthContext.AcquireTokenAsync(
                                        "https://graph.windows.net", 
                                        ClientCredential);

现在,如果我在 Azure Graph 中重用该 token ,我可以使用以下代码恢复所有组:

var url = $"https://graph.windows.net/myorganization/groups?api-version=1.6";
 var client = new HttpClient();
 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", resultAccessToken);
  var response = await client.GetAsync(url);
  if (response.Content != null) {
       Console.WriteLine(await response.Content.ReadAsStringAsync());
  }

根据文档,我应该使用较新的 Microsoft Graph API,但当我尝试使用以下代码时遇到一些问题:

var authenticationProvider = new DelegateAuthenticationProvider(
      req.Headers.Authorization = new AuthenticationHeaderValue("bearer", 
                                           resultAccessToken.AccessToken);
      return Task.CompletedTask;
});
var graph = new GraphServiceClient(authenticationProvider);
var groups = await graph.Groups
                    .Request()
                    .GetAsync();

每当我运行此程序时,我最终都会收到以下异常:

未处理的异常:System.AggregateException:发生一个或多个错误。 ---> Microsoft.Graph.ServiceException:代码:InvalidAuthenticationToken 消息:访问 token 验证失败。

很明显我在这里遗漏了一些东西,但是什么呢? (顺便说一句,我已经设法让所有内容都与 MSAL 库一起使用,但在这种情况下,我确实需要将我的组件与使用 ADAL 的其他组件集成),

谢谢。

路易斯

最佳答案

AAD Graph 和 Microsoft Graph 具有不同的物理端点。

要调用 AAD 图表,您需要带有受众声明的 token https://graph.windows.net

要调用 Microsoft Graph,您需要带有受众声明的 token https://graph.microsoft.com

这意味着您需要做两件事:

  1. 确保您的应用程序已配置到 Microsoft Graph。 (这意味着您需要更新应用程序的配置才能调用其他资源)
  2. 更新您的代码以请求正确资源的 token 。

希望这会有所帮助!

关于c# - 微软图: how to get all groups reusing the token generated through ADAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44416182/

相关文章:

node.js - 在 Node.js 中验证 Azure idToken

javascript - 无法使用 Azure AD 和 SPA 获取 token

c# - 使用 C# 更改 SciChart 3D 工具提示中的标签

c# - 如何在 NHibernate 中使用枚举?

c# - Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[48] 读取 key 环时发生错误

azure - 等待服务准备就绪 (Service Fabric)

azure-active-directory - 从元数据端点缓存颁发者和 key

c# - 在 C# 中从多行文本框的开头删除行

c# - 在 ASP.NET Webforms 应用程序的 web.config 中加载多个用户控件应用程序是个坏主意

azure - 如何为一个 Azure 网站保留实例配置多个主机 header