2016-11-11 110 views
1

我已經創建了一個API(Azure API應用程序),並使用Azure Active Directory啓用了身份驗證/ authtorization(來自應用程序API。應用程序服務在AAD中註冊,好爲止。Azure API應用程序訪問令牌(ADAL)不起作用

我已經按照以下職位的步驟生成令牌,但該令牌似乎並沒有工作。

var authContext = new AuthenticationContext("https://login.microsoftonline.com/<guid>/oauth2/authorize"); 
var credential = new ClientCredential("<clientId>", "<secret_from_aad>"); 
var result = (AuthenticationResult)authContext.AcquireTokenAsync("http://<api>.azurewebsites.net", credential).Result; 
var token = result.AccessToken; 

https://msdn.microsoft.com/en-us/library/azure/mt428036.aspx

的原始請求看起來像這個:

GET https://<api>.azurewebsites.net/rest/v1/crm/surveys/all HTTP/1.1 
Host: <api>.azurewebsites.net 
Connection: close 
Accept-Encoding: gzip,deflate 
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJ<rest-of-token...> 
Host: <api>.azurewebsites.net 
User-Agent: Apache-HttpClient/4.1.1 (java 1.5) 

的響應是

HTTP/1.1 401 Unauthorized 
Content-Length: 58 
Content-Type: text/html 
Server: Microsoft-IIS/8.0 
WWW-Authenticate: Bearer realm="<api>.azurewebsites.net" 
X-Powered-By: ASP.NET 
Set-Cookie: ARRAffinity=dd32cab21d0ca9541343a77c51d355d0781c0e0a4147a2166ecb955fe9d94a60;Path=/;Domain=<api>.azurewebsites.net 
Date: Fri, 11 Nov 2016 11:20:49 GMT 
Connection: close 

You do not have permission to view this directory or page. 

我掙扎了一會發現生成的令牌,而不在登錄頁面顯示的方式,我不知道這是最好的方法(簡單雖然...)。

使用API​​的系統必須能夠以編程方式生成令牌並與請求一起發送。

我創建了一個虛擬AAD,虛擬應用服務等,並放在一起這樣的代碼示例:

class Program 
    { 
     static void Main(string[] args) 
     { 
      string response = HttpRequest(); 
      Console.ReadLine(); 
     } 

     public static string HttpRequest() 
     { 
      string serviceURI = "https://apiappdemo.azurewebsites.net/api/values/"; 

      //Get the access token 
      string token = GetToken(); 

      HttpWebRequest request = System.Net.WebRequest.Create(serviceURI) as System.Net.HttpWebRequest; 
      request.KeepAlive = true; 
      request.Method = "GET"; 
      request.ContentLength = 0; 
      request.ContentType = "application/json"; 
      request.Headers.Add("Authorization", String.Format("Bearer {0}", token)); 

      using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse) //Failing here... 401 
      { 
       using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream())) 
       { 
        return reader.ReadToEnd(); 
       } 
      } 
     } 

     public static string GetToken() 
     { 
      var authContext = new AuthenticationContext("https://login.microsoftonline.com/b37d6c4c-012f-450c-81c7-406b6b584348/oauth2/authorize"); 
      var credential = new ClientCredential("7ed0dccb-ade7-4a5e-b286-32b66eb929d1", "1bVIoJyMHsbuYsfuJ7or6krbKvWw3kpKfp69jsQuilw="); 
      var result = (AuthenticationResult)authContext.AcquireTokenAsync("https://apiappdemo.azurewebsites.net", credential).Result; 
      return result.AccessToken; 
     } 
    } 

任何想法可能是錯誤的?

的問候, 邁克

回答

2

爲了使令牌適用於它通過Azure的AD保護的應用服務,我們需要使用高級管理模式(參見here)。

我們將在Azure AD中使用兩個應用程序reigster,第一個用於保護應用程序服務。其次是用作客戶端來請求重新分配。我們需要指定允許的令牌受衆,這是應用程序的應用程序ID URI(第一個應用程序)。下面是一個供您參考的圖: enter image description here

然後,我們可以使用第二個應用程序作爲客戶端來獲取app應用程序服務的令牌。並確保在AcquireTokenAsync方法中您確定的重新配置是預覽設置中的audience配置。

+0

不知道我跟着你在這裏,爲什麼我需要兩個應用程序,並會是什麼其他應用程序實際上是? – Mike

+0

Azure中有兩個概念。首先是需要保護的資源。其次是需要訪問受保護資源的客戶端。第一個應用程序表示使用應用程序服務的身份驗證/授權功能對其進行配置時的資源。然後,第二個應用程序代表您使用其客戶端ID,客戶端祕密和第一個應用程序的應用程序標識URI(它將作爲標記中的「aud」聲明發布)以獲取第一個應用程序的標記。 –

1

替換此行:

authContext.AcquireTokenAsync("https://apiappdemo.azurewebsites.net" 
            ,credential).Result; 

有:

authContext.AcquireTokenAsync("7ed0dccb-ade7-4a5e-b286-32b66eb929d1" 
            ,credential).Result; 

使用您的客戶端ID作爲資源ID

+1

雖然這可能回答這個問題,但最好解釋答案的基本部分,並可能是OP代碼的問題。 – pirho

相關問題