簡體   English   中英

401 未經授權的 Azure 管理 API

[英]401 unauthorized Azure management api

我正在調用 azure management api 來獲取雲服務中的實例列表。

以下代碼在 fiddler 處於活動狀態時有效,並且在未通過 fiddler 時不斷返回 401 未經授權。

我無法確定原因。 沒有 Fiddler,令牌會正確返回,因此,我不明白為什么我會收到未經授權的令牌。

private HostedService GetCloudServiceProperties(string serviceName)
{
       if (_client == null)
        {
            GetClient();
        }

        string serviceProperties = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}?embed-detail=true", AzureSettings.SubscriptionId, serviceName);
        var result = _client.GetStringAsync(serviceProperties).Result;
        var serializer = new XmlSerializer(typeof(HostedService), "http://schemas.microsoft.com/windowsazure");
        var returnValue = (HostedService)serializer.Deserialize(new StringReader(result));
        return returnValue;
}

private void GetClient()
{
        string authority = "https://login.microsoftonline.com/" + AzureSettings.TenantName;
        IConfidentialClientApplication app;
        app = ConfidentialClientApplicationBuilder.Create(AzureSettings.ClientId)
                                                  .WithClientSecret(AzureSettings.ClientSecret)
                                                  .WithAuthority(new Uri(authority))
                                                  .Build();

        List<string> scopes = new List<string>()
        {
            "https://management.core.windows.net/.default"
        };
        var token = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;
        _client = new HttpClient();
        _client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
        _client.DefaultRequestHeaders.Add("x-ms-version", "2009-10-01");
}

簡短的回答,但我覺得解決方案是將不記名資本化。 服務通常期望 'Bearer',所以這可能是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM