2017-04-25 189 views
0

如何使用Azure AD獲取應用程序令牌以查詢具有應用程序憑據(=無用戶模擬)的SharePoint?使用Azure AD令牌應用程序訪問SharePoint Online

下面的代碼完全適用於查詢數據的用戶,但我們需要不帶模擬像列出集合中的所有網站,不考慮用戶權限等

拋出異常獲取信息:

的 「Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException」 發生在mscorlib.dll中,但未在用戶代碼中處理

附加信息:AADS TS70001:與標識符 'XXX' 應用目錄sharepoint.com未找到

代碼來獲得令牌:

internal static async Task<string> GetSharePointAccessToken(string url, string userAccessTokenForImpersonation) 
      { 

      string clientID = @"<not posted on stack overflow>"; 
      string clientSecret = @"<not posted on stack overflow>"; 

      var appCred = new ClientCredential(clientID, clientSecret); 
      var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/common"); 

      // Use user assetion if provided, otherwise use principal account 
      AuthenticationResult authResult = null; 

      if (string.IsNullOrEmpty(userAccessTokenForImpersonation)) 
      { 
       authResult = await authContext.AcquireTokenAsync(new Uri(url).GetLeftPart(UriPartial.Authority), appCred); 
      } 
      else 
      { 
       authResult = await authContext.AcquireTokenAsync(new Uri(url).GetLeftPart(UriPartial.Authority), appCred, new UserAssertion(userAccessTokenForImpersonation)); 
      } 

      return authResult.AccessToken; 
     } 

測試代碼:

// Auth token from Bearer https://xxx.azurewebsites.net/.auth/me 
string authHeader = @"<valid jwt bearer token from azure auth>"; 
var sharePointUrl = @"https://xxx.sharepoint.com/sites/testsite/"; 

string sharePrincipalToken = await GetSharePointAccessToken(sharePointUrl, null); // <-- doesn't work 
string sharePointUserToken = await GetSharePointAccessToken(sharePointUrl, authHeader); // <-- works 

Azure AD權限:

Permissions in Azure AD

回答

0

你所得到的錯誤消息意味着您正在使用指向我們的令牌服務獲得令牌的「sharepoint.com」的背景下,用戶登錄

這是因爲你正在使用「通用」端點。閱讀更多關於here

請嘗試使用固定端點,其中租戶與註冊應用程序的位置相同,並查看是否解決了您的問題。

如果你的計劃是使這個應用程序通過多租戶訪問,請確保您已明確設置你的應用程序是多租戶,並確保你有從外部租戶用戶試圖登錄到應用在嘗試對服務呼叫進行服務之前。

讓我知道這是否有幫助。

相關問題