2017-03-01 82 views
2

我試圖創建將在AWS託管一個新的Web應用程序。此應用程序需要通過OAuth2從我們的Azure Active Directory中對用戶進行身份驗證。這是我到目前爲止工作,我曾經讓我有步驟:使用OAuth2以對Azure的AD身份驗證調用的WebAPI

1)我可以生成從「login.microsoftonline.com」在用戶登錄後一個「代碼」。爲此,我在Azure AD中建立了一個新的應用程序,我的Web應用程序指導用戶登錄。我還在Azure AD中設置了API應用程序,該應用程序用作查詢字符串中的「resource」參數當我直接用戶使用「代碼」的login.microsoftonline.com端點

2)從上面#1產生的,我可以通過調用我的應用程序的/令牌端點生成我授權令牌。我可以通過傳遞相同的資源值(網址到我最終想使用的API),代碼,我的客戶端ID和我的客戶端密鑰來完成此操作。

3)來自#2的令牌響應上面發送的token_type,expires_in,scope,access_token,refresh_token和id_token屬性都有值。我能夠將id_token解碼爲JWT,並且它在索賠對象中顯示登錄用戶的正確用戶信息。

4)這裏是我的失敗然後我嘗試打電話給正在使用我獲得#3的上方傳遞該值在「授權」報頭與該值是「承載XYZ123 ......」如果我不把任何授權的access_token也登記在AD天青我的API的應用程序在API應用程序中,我得到了我期望的結果,但是,如果我在該類或Get()方法上放置了[Authorize]屬性,我總會得到401(未授權)。我確定還有其他東西需要連線,我只是不確定是什麼。我發現這個資源:https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-protect-backend-with-aad但會談API管理註冊我的API的,我不認爲我需要做的(我絕對不希望,如果我沒有)。

我創建將依賴於能夠獲得記錄在其中,我假設我可以承載令牌提取用戶的身份API,我只是不知道如何...

任何幫助和指導將不勝感激。

編輯包括工作解決方案: 下面是我在我的啓動類中使用的每個接受的答案下面。請注意,「受衆」值是我需要訪問的API端點的URL。租戶是我們在組織中綁定的自定義url,如果您沒有提供有效的租戶值,則可能會收到一個異常,其中顯示「響應狀態代碼不表示成功:404(未找到)」。你需要在Azure Active Directory的組件,你可以從得到的NuGet:Install-Package Microsoft.Owin.Security.ActiveDirectory

public partial class Startup 
{ 
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    { 

     app.UseWindowsAzureActiveDirectoryBearerAuthentication(
      new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
      { 
       Audience = "https://myapi.azurewebsites.net/", 
       Tenant = "my.custom.url.com", 
       TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters 
       { 
        ValidateIssuer = false 
       } 
      }); 
    } 
} 

這裏是我使用,使從需要訪問收到我的令牌應用程序的請求後的參數。

 body.Add(new KeyValuePair<string, string>("grant_type", "authorization_code")); 
     body.Add(new KeyValuePair<string, string>("code", code)); // code from response 
     body.Add(new KeyValuePair<string, string>("redirect_uri", "http://localhost:51015/redirect")); 
     body.Add(new KeyValuePair<string, string>("client_id", "xxxxxxx-8829-4294-b2c9-xxxxxxxxxx")); // client id of this application making the request 
     body.Add(new KeyValuePair<string, string>("client_secret", "PxxxxxxxxxxxxSnTJ4Uh63Voj+tkxxxxxxx=")); 
     body.Add(new KeyValuePair<string, string>("resource", "https://myapi.azurewebsites.net/")); // same value goes here that is in the audience value of the api, this is also the same value that is passed in the resource parameter of the query string on the redirect to the login to obtain the "code" in the previous step 

回答

3

這取決於你如何保護網絡API。通常情況下,我們可以使用Azure的廣告中使用了下面的代碼保護的Web API:

public partial class Startup 
{ 
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    { 

     app.UseWindowsAzureActiveDirectoryBearerAuthentication(
      new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
      { 
       Audience = ConfigurationManager.AppSettings["ida:Audience"], 
       Tenant = ConfigurationManager.AppSettings["ida:Tenant"], 
       TokenValidationParameters= new System.IdentityModel.Tokens.TokenValidationParameters { 
        ValidateIssuer=false 
       } 
      }); 
    } 
} 

觀衆是應用程序的客戶端Id你在Azure門戶註冊。在這種情況下,我們使用這個應用程序作爲客戶端和資源。然後,我們canrequest訪問令牌像下面的要求:

POST: https://login.microsoftonline.com/{tenantId}/oauth2/token 
resource={clientId}&client_id={clientId}&code={authorizationCode}&grant_type=authorization_code&redirect_uri={redirectUri}&client_secret={clientSecret} 

此標記應爲網絡API作爲通過上面的代碼保護的授權作品。有關保護Web API的更多細節,請參閱here

+1

請注意POST請求中的兩個客戶端ID不一樣。 'resource'是API的客戶端ID,'client_id'是調用應用程序的客戶端ID。另外,我發現您可以使用客戶端ID或API的資源URI來請求令牌。儘管令牌中的觀衆將是客戶端ID。 – juunas

+0

我想我已經嘗試了受衆部分和查詢字符串中的客戶端ID,資源(URI)等的每個組合,以檢索該令牌並仍然獲得401s。我不確定哪個應用程序的客戶端ID等位置在哪裏(甚至使用@juunas註釋作爲指南。你能說出究竟需要去哪裏嗎?我還應該使用GUID作爲客戶端ID還是應該使用一個URI –

+1

沒關係,我已經改變了我作爲一個測試通過的令牌,但我現在已經開始工作了。非常感謝你們兩位的幫助!+ 1對你們來說:) –

0

入住提琴手任何其他詳細信息」相關的認證失敗,有可能是像信息‘無效的觀衆’,等你需要知道失敗(401錯誤)來解決這個問題的根本原因。

相關問題