2016-11-12 43 views
0

API AppをAzureにデプロイしましたが、認証(AAD付き)がONに設定されている場合、APIクライアントの作成に問題があります。Azure Active Directory認証を使用してAzure REST APIアプリケーションを使用する方法

サービスクライアントを生成しようとすると(認証がオフの場合)、クライアントコードが生成され(Autorestで完了しています)、コードは機能しますが、認証をオンにすると(要求が認証されない場合>追加 - - >(プロジェクトのコンテキストメニューから)Login with Azure Active Directoryに設定し、その後、

1)サービスコール)は、AADのログインページにリダイレクトせず(

2を401 Unauthorizedを返された)それから私はもう一度サービス・クライアントを生成しようとしますREST API Client - >ダイアログボックスで「Azure Assetを選択」を選択し、OKを押してメッセージを受け取りました"Failed to download metadata file for Microsoft Azure API App: ...app name..."(「追加情報なし利用可能」)

I)は急行の設定を使用して(この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/

任意の提案を働いていないのですか?私は要求URLを入力した場合

EDIT

1)(そのREST APIクライアントは、Webブラウザで)使用しています - それは正当な結果 2を返します)私は、私が資格情報なしのREST APIを使用していていることが分かりましたAADのログイン画面になったが、CREを入力した後 -

EDIT 2

私はいくつかの進歩を得ました(私は、AzureのADのログイン画面が、この場合に提示しなければならないと思った...しかし、そうではありません)

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

はこれらは私がここまで取得するためにやった手順です:私はbearer tokenを得るが、私はサービスを照会しようとすると、私はエラーメッセージが表示されますdentials

0)が追加されましたMicrosoft.IdentityModel.Clients。クライアントアプリケーションからREST APIを呼び出すときに、私は4を提供ServiceClientCredentialsを作成する際のAzure Active Directoryに

2を私のクライアントアプリを登録したクライアントのプロジェクト

1にnugetパック)のActiveDirectory)、Iは)ServiceClientCredentials

3を追加してい要素 -authority =これは、AADアプリ登録からである - >エンドポイント=>(開始部分http://login.windows.net/せず)フェデレーションメタデータドキュメントvērtība

-resource =>これは、REST APIのURI(対象のRE =>識別子であります

-clientId =>これは、クライアントアプリケーションがネイティブアプリケーションであるため、クライアントアプリケーションをAADに登録した後に取得します。 -redirect Uri =>任意の有効なURL

クライアントアプリケーションでこのリソースを指定するにはどうすればよいですか?

client has not specified this resource in its requiredResourceAccess list

答えて

1

私はAzureのREST APIアプリケーションにAAD認可を有効にする方法について解決策を見つけることができました。誰もが同じ挑戦を持っている場合に備えて、私はこれが役立つことを願っています。要求が認証されていない場合に取る>認証/認可

  • アプリケーションサービスの認証=>
  • アクション -

    1)アプリケーションサービスで:

    これらは私がやった手順は次のとおりです=> AADでログイン

  • Express設定のAADを設定しました(そこには、Azure AD AppをAPIアプリケーション用に作成する必要があります - つまり、あなたのservの "App registration"氷)のAzure Active Directoryに

2) - あなたはRESTに関する情報を追加する必要がありますrequiredResourceAccessセクションで - >アプリケーションの登録

  • は、クライアントアプリのクライアントアプリの
  • 編集マニフェストを登録しますAPIアプリケーション:
    • resourceAppId - >ここにREST APIアプリケーションIDを挿入
    • resourceAccess {id} - > OauthPer REST APIのミッションID値(REST APIのマニフェストで取得できます!クライアントアプリケーションで

    3)

  • 生成Autorestは(ソリューションエクスプローラから使用して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.このリソースは、解決策を見つけるために必要なもののかなりの部分を私に与えました。http://developers.de/blogs/damir_dobric/archive/2016/05/24/aad-authentication-with-rest-api-client.aspx – Prokurors

関連する問題