azure - 从 Azure 托管的 .Net Core 3.1 应用程序访问 Kroger API 时出现 403 禁止

标签 azure .net-core https httpclient

问题:我需要确定此故障的性质,以便知道如何进一步排除故障。我提出了一些假设: - 可能是 Azure 中的防火墙/代理配置 - 可能是 Kroger 的 API 配置错误 - 可能是 Azure 应用程序拒绝公共(public)证书 - 可能与上述任何内容完全无关

详细信息:我正在尝试连接到 Kroger 的开发人员 API。本文已简化以下代码。 (我之前一直使用 IHttpClientFactory 来生成我的 HTTPClient)。这在本地工作,但是一旦将其部署到 Azure Web 服务,我就会收到一条 403 消息(该消息似乎来自 Azure,而不是外部 API):

您无权访问此服务器上的 http://api.kroger.com。

同样的代码在 Azure 中适用于通过 HTTPS 的其他第 3 方 API,因此我怀疑此错误来自于未使用正确的客户端证书,因此 Azure 尝试通过 http 进行调用?

我尝试了很多方法来解决此问题,包括上传我从 https://api.kroger.com 下载的公共(public)证书。到我的 azure 应用程序服务。它似乎正确提取了证书,但请求仍然失败并显示相同的消息。

相关代码如下(不使用客户端证书):

using(var client = _requestFactory.CreateClient()))
{
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("basic", _clientId);
        client.DefaultRequestHeaders.Add("Accept", "application/json");

        var newRequest = new Dictionary<string, string>
        {
            { "grant_type", "client_credentials" },
            { "scope", "product.compact" }
        };

        var response = await client.PostAsync($"https://api.kroger.com/v1/connect/oauth2/token", new FormUrlEncodedContent(newRequest));
        return Ok(await response.Content.ReadAsStringAsync());
}

以下是来自服务器的完整响应。

{
    "version": "1.1",
    "content": {
        "headers": [{
            "key": "Content-Type",
            "value": ["text/html"]
        }, {
            "key": "Content-Length",
            "value": ["299"]
        }, {
            "key": "Expires",
            "value": ["Sat, 08 Feb 2020 19:18:55 GMT"]
        }]
    },
    "statusCode": 403,
    "reasonPhrase": "Forbidden",
    "headers": [{
        "key": "Server",
        "value": ["AkamaiGHost"]
    }, {
        "key": "Mime-Version",
        "value": ["1.0"]
    }, {
        "key": "Date",
        "value": ["Sat, 08 Feb 2020 19:18:55 GMT"]
    }, {
        "key": "Connection",
        "value": ["close"]
    }, {
        "key": "Set-Cookie",
        "value": ["akaalb_Digital_ALB_API=~op=KT_Digital_API_KCVG_F5:api-kcvg|~rv=47~m=api-kcvg:0|~os=75b4a9ec926d2a9e67035451773cec6c~id=63ba4b3e2a027e4d53b693e2fded5ac3; path=/; HttpOnly; Secure; SameSite=None"]
    }],
    "trailingHeaders": [],
    "requestMessage": {
        "version": "1.1",
        "content": {
            "headers": [{
                "key": "Content-Type",
                "value": ["application/x-www-form-urlencoded"]
            }, {
                "key": "Content-Length",
                "value": ["51"]
            }]
        },
        "method": {
            "method": "POST"
        },
        "requestUri": "https://api.kroger.com/v1/connect/oauth2/token",
        "headers": [{
            "key": "Authorization",
            "value": ["basic {removed}"]
        }, {
            "key": "Accept",
            "value": ["application/json"]
        }, {
            "key": "Request-Context",
            "value": ["appId={removed}"]
        }, {
            "key": "Request-Id",
            "value": ["|{removed}"]
        }, {
            "key": "traceparent",
            "value": ["{removed}"]
        }],
        "properties": {}
    },
    "isSuccessStatusCode": false
}

最佳答案

"headers": [{ "key": "Server", "value": ["AkamaiGHost"] }

几乎肯定是 Akamai 问题。我见过由于 header 顺序甚至 Akamai 不喜欢的用户代理 header 而导致的 403 错误。

关于azure - 从 Azure 托管的 .Net Core 3.1 应用程序访问 Kroger API 时出现 403 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60130527/

相关文章:

vb.net - 如何将我的 webrole 应用程序 (vb.net) 连接到我的 sql azure 数据库?

c# - .NET Entity Framework 核心

angular - NPM 脚本 'start' 退出时没有指示 Angular CLI 正在监听请求

c# - 是什么导致这个 HTTPS WebRequest 即使在浏览器中工作也会超时?

java - IPCZQ cookie httpOnly

c# - 表单和 Azure AD 身份验证

Azure KQL - 在时间表之上创建总平均线

mysql - Azure mySQL 密码已过期

node.js - express 虚拟主机 + https

linux - .NET Core 和 Kestrel - 部署在 Linux 上的 ASP.NET CORE Web api 应用程序的反向代理 Web 服务器应该是什么?