Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Microsoft Graph:如何让所有组重用通过ADAL生成的令牌_C#_Azure_Azure Active Directory_Microsoft Graph Api_Adal - Fatal编程技术网

C# Microsoft Graph:如何让所有组重用通过ADAL生成的令牌

C# Microsoft Graph:如何让所有组重用通过ADAL生成的令牌,c#,azure,azure-active-directory,microsoft-graph-api,adal,C#,Azure,Azure Active Directory,Microsoft Graph Api,Adal,我正在尝试使用MicrosoftGraph,我在这里有点迷路了。我正在使用ADAL通过AuthenticationContext.AcquireTokenAsync方法验证我的应用程序(应用程序id+密码)。代码如下所示: AuthenticationContext AuthContext = new AuthenticationContext(Authority); ClientCredential ClientCredential = new ClientCredential(ClientI

我正在尝试使用MicrosoftGraph,我在这里有点迷路了。我正在使用ADAL通过AuthenticationContext.AcquireTokenAsync方法验证我的应用程序(应用程序id+密码)。代码如下所示:

AuthenticationContext AuthContext = new AuthenticationContext(Authority);
ClientCredential ClientCredential = new ClientCredential(ClientId, AppKey);
var result = await AuthContext.AcquireTokenAsync(
                                        "https://graph.windows.net", 
                                        ClientCredential);
现在,如果我将令牌与Azure Graph一起重用,我可以使用以下代码恢复所有组:

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 消息:访问令牌验证失败

很明显,我遗漏了一些东西,但是什么?(顺便说一句,我已经设法让MSAL库的一切都正常工作,但在这种情况下,我真的需要让我的组件与使用ADAL的其他组件集成)

谢谢


Luis

AAD图形和Microsoft图形具有不同的物理端点

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

要调用Microsoft Graph,您需要一个带有访问者声明的令牌
https://graph.microsoft.com

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

  • 确保应用程序已配置为Microsoft Graph。(这意味着您需要更新应用程序的配置以调用其他资源)
  • 更新代码以请求正确资源的令牌

  • 我希望这有帮助

    AAD图形和Microsoft图形具有不同的物理端点

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

    要调用Microsoft Graph,您需要一个带有访问者声明的令牌
    https://graph.microsoft.com

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

  • 确保应用程序已配置为Microsoft Graph。(这意味着您需要更新应用程序的配置以调用其他资源)
  • 更新代码以请求正确资源的令牌
  • 我希望这有帮助