簡體   English   中英

Microsoft Graph:如何讓所有組重用通過ADAL生成的令牌

[英]Microsoft Graph: how to get all groups reusing the token generated through ADAL

我正在嘗試使用Microsoft Graph,但在這里我有點迷路了。 我正在使用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的組件集成在一起),

謝謝。

路易斯

AAD圖和Microsoft圖具有不同的物理端點。

要調用AAD圖,您需要帶有受眾聲明https://graph.windows.net的令牌

要調用Microsoft Graph,您需要帶有受眾聲明的令牌https://graph.microsoft.com

這意味着您需要做兩件事:

  1. 確保將您的應用程序配置為Microsoft Graph。 (這意味着您需要更新應用程序的配置以調用其他資源)
  2. 更新您的代碼以請求令牌以獲取正確的資源。

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM