【问题标题】:401 unauthorized Azure management api401未经授权的Azure管理api
【发布时间】:2020-03-31 13:47:12
【问题描述】:

我正在调用 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");
}

【问题讨论】:

    标签: c# fiddler azure-authentication azure-management-api


    【解决方案1】:

    简短的回答,但我觉得解决方案是大写不记名。服务通常需要“Bearer”,所以这可能是问题所在。

    【讨论】:

    • 不幸的是没有。与 Bearer 一起尝试过。它还返回 401。我编辑了问题
    • 用证书替换了身份验证。同样的问题。错误是 WWW-Authenticate: Bearer error="invalid_token", error_description="JWT 令牌不包含预期的受众 uri 'management.core.windows.net/'。" }。但范围包含此网址
    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 2020-04-10
    • 1970-01-01
    • 2019-03-07
    • 2017-09-27
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多