2017-03-29 12 views
0

Azure Schedulerを呼び出してトークンを取得した後、そのトークンを使用して安らかな呼び出しを行うコードがあります。治療をしなさい。Azure Web API Unauthorized 401

私はこのコードを新しいアプリケーションに採用することに決めましたが、今度は自分のWeb APIを青空のホストとして呼びます。 APIはActive Directoryに登録されています。私は秘密鍵などを作成しました。私の静的なHTTPクライアントをinitiliazeすると、トークンが首尾よく取得されます。

しかし、authのトーク​​ンを使用してAPIを呼び出すと、レスポンスは401 "unauthorized"となり、コードは以下のようになります。

public static class SchedulerHttpClient 
{ 
    const string SPNPayload = "resource={0}&client_id={1}&grant_type=client_credentials&client_secret={2}"; 

    private static Lazy<Task<HttpClient>> _Client = new Lazy<Task<HttpClient>>(async() => 
    { 
     string baseAddress = ConfigurationManager.AppSettings["BaseAddress"]; 
     var client = new HttpClient(); 
     client.BaseAddress = new Uri(baseAddress); 
     await MainAsync(client).ConfigureAwait(false); 
     return client; 
    }); 

    public static Task<HttpClient> ClientTask => _Client.Value; 

    private static async Task MainAsync(HttpClient client) 
    { 
     string tenantId = ConfigurationManager.AppSettings["AzureTenantId"]; 
     string clientId = ConfigurationManager.AppSettings["AzureClientId"]; 
     string clientSecret = ConfigurationManager.AppSettings["AzureClientSecret"]; 

     string token = await AcquireTokenBySPN(client, tenantId, clientId, clientSecret).ConfigureAwait(false); 

     client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); //TODO ssmith: const or localization 
    } 

    private static async Task<string> AcquireTokenBySPN(HttpClient client, string tenantId, string clientId, string clientSecret) 
    { 
     var payload = String.Format(SPNPayload, 
            WebUtility.UrlEncode(ConfigurationManager.AppSettings["ARMResource"]), 
            WebUtility.UrlEncode(clientId), 
            WebUtility.UrlEncode(clientSecret)); 

     var body = await HttpPost(client, tenantId, payload).ConfigureAwait(false); 
     return body.access_token; 
    } 

    private static async Task<dynamic> HttpPost(HttpClient client, string tenantId, string payload) 
    { 
     var address = String.Format(ConfigurationManager.AppSettings["TokenEndpoint"], tenantId); 
     var content = new StringContent(payload, Encoding.UTF8, "application/x-www-form-urlencoded"); 
     using (var response = await client.PostAsync(address, content).ConfigureAwait(false)) 
     { 
      if (!response.IsSuccessStatusCode) 
      { 
       Console.WriteLine("Status: {0}", response.StatusCode); 
       Console.WriteLine("Content: {0}", await response.Content.ReadAsStringAsync().ConfigureAwait(false)); 
      }//TODO: start removing tests 

      response.EnsureSuccessStatusCode(); 

      return await response.Content.ReadAsAsync<dynamic>().ConfigureAwait(false); 
     } 
    } 
} 

上記のコードは、httpclientを作成し、その権限を取得するクラスです。

​​

上記のコードは、私が結果を得ることができるかどうかを単にテストするために私の古いコードを簡単に持ち上げたものです。しかし述べたように、それは401を返します。

私の古い質問は、間違った認可を得ることですか?

<add key="ARMResource" value="https://management.core.windows.net/" /> 
<add key="TokenEndpoint" value="https://login.windows.net/{0}/oauth2/token" /> 
<add key="BaseAddress" value="https://mysite.azurewebsites.net" /> 

答えて

0

この特定の問題は、Web APIの場合、不正な「ARMリソース」によってクライアントIDに変更する必要があると思われます。

Source of answer

は、しかし、私は私SPNPayload文字列から完全にリソースを省略することができるかもしれ疑う、私の問題は同じであったようです。