2017-09-10 79 views
2

我按照本教程(https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-integrating-applications)創建OAuth應用程序以將新用戶添加到我的Azure雲訂閱。Active Directory集成應用程序:訪問令牌無效

正如本教程所述,我創建了我的應用程序,分配了權限並執行了創建的密鑰。

我在做什麼是相當「標準」,但它仍然不起作用。

讓我告訴步驟:

https://login.microsoftonline.com/common/oauth2/authorize?client_id=[CLIENTID]&response_type=code&redirect_uri=[REDIRECT_URI]&prompt=admin_consent 

登錄我成功地去:

Permission Request

此驗收後,我讓我的服務,我撰寫我的「最後一步」,在代碼驗證以檢索令牌

  var content = new StringContent(
      "grant_type=authorization_code" + 
      "&client_id=" + Connectors.Azure.AzureHelper.ID + 
      "&client_secret=" + Connectors.Azure.AzureHelper.Secret + 
      "&code=" + code + 
      "&resource=" + Connectors.Azure.AzureHelper.ID + 
      "&redirect_uri=" + Request.Url.AbsoluteUri.Split('?')[0], 
      Encoding.UTF8, "application/x-www-form-urlencoded"); 

      var resp = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/token", content); 
      var text = await resp.Content.ReadAsStringAsync(); 

      var token = JsonConvert.DeserializeObject<Connectors.Office365.AuthResp>(text); 

In token.access_token我有一個「格式良好」的標記。

在token.Scopes我也有很多「授予的權限」,如:

Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All Group.Read.All Group.ReadWrite.All Member.Read.Hidden User.Read User.Read.All User.ReadBasic.All 

但如果我嘗試以執行類似操作最簡單:

Error on get users

這就像我正在檢索有效令牌,但沒有功能! 什麼可能是錯的?

在「代碼確認」的資源字段中,輸入了我的應用程序的ID。那是對的嗎? 我還可以嘗試什麼?

回答

0

也許我會發現哪裏出了問題。一切都是關於令牌請求中的「資源」字段。

現在我的登錄URL指定我想爲https://graph.api.net令牌:

var myUrl = "https://login.microsoftonline.com/common/oauth2/authorize?client_id=" + AzureHelper.ID + "&response_type=code&redirect_uri=" + baseUrl + "/Account/OAuth&prompt=admin_consent&resource=" + Uri.EscapeDataString("https://graph.windows.net"); 

,讓我這個網址:

https://login.microsoftonline.com/common/oauth2/authorize?client_id=[ID]&response_type=code&redirect_uri=[URL]&prompt=admin_consent&resource=https%3A%2F%2Fgraph.windows.net 

然後,在代碼驗證我請求相同的資源:

  var content = new StringContent(
      "grant_type=authorization_code" + 
      "&client_id=" + Connectors.Azure.AzureHelper.ID + 
      "&client_secret=" + Connectors.Azure.AzureHelper.Secret + 
      "&code=" + code + 
      "&resource=" + Uri.EscapeDataString("https://graph.windows.net")+ //Connectors.Azure.AzureHelper.ID + 
      "&redirect_uri=" + Request.Url.AbsoluteUri.Split('?')[0], 
      Encoding.UTF8, "application/x-www-form-urlencoded"); 

      var resp = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/token", content); 
      var text = await resp.Content.ReadAsStringAsync(); 

      var token = JsonConvert.DeserializeObject<Connectors.Office365.AuthResp>(text); 

而且一切都像魅力一樣。

我的具體問題是由於這樣一個事實,即在第一次測試中,我試圖將Request參數放在沒有進行URL編碼的位置。

奇怪的事實是,對於「REDIRECT_URI」你不要在你需要它的「請求」

+1

待辦事項需要特定的編碼,資源URI'https://graph.windows.net '適用於Azure AD Graph API,而'https:// graph.microsoft.com'適用於Microsoft Graph API。 – juunas

相關問題