2016-11-12 31 views
0

我已經部署了API App天青,但我有,如果驗證(與AAD)設置爲ON創建API客戶端的問題。如何飲用Azure的REST API應用程序與Azure的Active Directory的授權在

當我嘗試生成服務客戶端(當身份驗證爲OFF時),然後生成客戶端代碼(使用Autorest完成)並且代碼正常工作,但是當我切換身份驗證開啓時(以及在請求未通過身份驗證時採取的操作被設置爲Login with Azure Active Directory),然後

1)服務調用返回401 Unauthorized(不重定向到AAD登錄頁)

2)然後我試圖生成服務端一次(從項目的上下文菜單 - >添加 - > REST API客戶端 - >然後在對話框中選擇「選擇Azure資產」,然後按「確定」,並收到消息"Failed to download metadata file for Microsoft Azure API App: ...app name..."(和「沒有附加信息可用「)

我(使用Express設置)根據本Azure的手動實現AAD:

https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-how-to-configure-active-directory-authentication/

根據這個視頻時,也和一切什麼是在這個視頻中所示是工作,除了AAD未證實......對我來說不工作...

https://azure.microsoft.com/en-us/documentation/videos/connect-2015-what-s-new-in-app-service-api-apps/

有什麼建議?

編輯

1)如果我輸入請求的URL(即REST API客戶端使用)的網絡瀏覽器 - 然後返回結果有效 2)我發現我使用REST API無憑據(我認爲Azure的AD登錄屏幕應該在這種情況下提出的...但它不是)

EDIT 2

我得到了一些進步 - 到了AAD登錄屏幕,但進入後華創dentials我得到的bearer token,但是當我嘗試查詢服務,我得到一個錯誤信息:

AADSTS65005: The client application has requested access to resource 'https....azurewebsites.net'. This request has failed because the client has not specified this resource in its requiredResourceAccess list. Trace ID: 4176e... Correlation ID: 1d612d... Timestamp: 2016-11-13 18:28:34Z

這些是遠遠得到這個我已經做了步驟:

0)添加了Microsoft.IdentityModel.Clients。ActiveDirectory中的NuGet包到客戶端項目

1)登記在Azure中的Active Directory我的客戶端應用程序

2)從客戶端應用程序調用REST API的時候,我加入ServiceClientCredentials

3)創建ServiceClientCredentials當我提供4元件 -authority =這是從AAD應用註冊 - >端點=>聯邦元數據文檔vērtība(無起始部分http://login.windows.net/

-resource =>這是REST API URI(=>目標資源的標識符這是收到的請求令牌的收件人)

-clientId =>這是我註冊客戶端應用程序在AAD後得到的應用程序ID -redirect Uri =>因爲我的客戶端應用程序是本機應用程序,那麼這只是任何有效的網址

如何在我的客戶端應用程序中指定此資源?

client has not specified this resource in its requiredResourceAccess list

回答

1

我設法找到如何使AAD授權Azure的REST API應用程序的解決方案。以防萬一任何人有相同的挑戰,我希望這會有所幫助。

這些是我沒有以下步驟:

1)在應用程序的服務 -

  • 應用服務認證=>開
  • 採取的措施時請求沒有被認證>認證/授權=>使用AAD登錄
  • 使用快速設置配置AAD(您必須爲您的API應用程序創建Azure AD App - 即爲您的服務器提供「應用程序註冊」冰)

2)在Azure中的Active Directory - >應用註冊

  • 添加註冊爲您的客戶端應用程序
  • 您的客戶端應用程序的編輯清單 - 在requiredResourceAccess部分必須添加關於REST的信息API應用程序:
    • resourceAppId - >在此處插入REST API應用程序ID
    • resourceAccess {id} - > OauthPer REST API的任務ID值(您可以在REST API的清單中找到它!)

3)在客戶端應用程序

  • 生成使用Autorest(從溶液Explorer的REST客戶端:Add\REST API client)或手動創建它
  • 添加Microsoft.IdentityModel.Clients.ActiveDirectory的NuGet包
  • 獲取並使用令牌以類似於此的代碼訪問您的API:

    //request 
        (..) 
        var tokenCreds = getToken(); 
        ServiceClientCredentials credentials = tokenCreds; 
    
        using (var client = new YourAPI(credentials)) { 
        ... 
        } 
        (..) 
    
        //getting token 
    
    private static TokenCredentials getToken() 
    { 
        //get this from Federation Metadata Document in 
        //Azure Active Directory App registrations -> Endpoints 
        var authority = "f1..."; 
    
        //Identifier of the target resource that is the recipient of the requested token 
        var resource = "https://yourapi.azurewebsites.net"; 
    
        //client application id (see Azure Active Directory App registration 
        //for your client app 
        var clientId = "a71..."; 
    
        //return url - not relevant for Native apps (just has to be valid url) 
        var redirectUri = "https://just-some-valid-url.net"; 
    
        AuthenticationContext authContext = 
        new AuthenticationContext(string.Format 
        ("https://login.windows.net/{0}", 
    authority)); 
    
        AuthenticationResult tokenAuthResult = 
        authContext.AcquireTokenAsync(resource, 
        clientId, 
        new Uri(redirectUri), 
        new PlatformParameters(PromptBehavior.Auto)).Result; 
    
        return new TokenCredentials(tokenAuthResult.AccessToken); 
    } 
    
+0

P.S.此資源爲我提供了查找解決方案所需的大部分資源http://developers.de/blogs/damir_dobric/archive/2016/05/24/aad-authentication-with-rest-api-client.aspx – Prokurors

相關問題