从Azure API管理调用时X-ARR-ClientCert标头中没有证书

Ron*_*erg 5 authentication azure azure-web-sites azure-api-management

我已经配置了一个Azure网站(带有一个ApiController),以按照此处提供的说明使用客户端证书身份验证。总结:您将clientCertEnabled标志设置为true,然后在您的网站上开始请求客户端身份验证证书。

效果很好,但是,现在我要访问发送到服务器的客户端证书。根据MSDN文章,它应该在X-ARR-ClientCert请求标头中可用,除了它不是!!

这意味着具有客户端身份验证证书的任何人都可以访问我的API,在我看来,这是不可取的。

那么,如何检索客户端发送到我的Web API的客户端身份验证证书?

更新1:我实际上是通过Azure API管理调用我的API 。我使用客户端身份验证证书配置了APIM,APIM调用了我的API,没有任何问题。但是,从APIM调用API时,未设置X-ARR-ClientCert标头。当直接通过Fiddler调用时,我确实看到了标头。因此,APIM以某种不同的方式调用我的API吗?

更新2:我再次经历了所有事情,并产生了一些日志记录。首先,DelegatingHandler我要从中记录该类的相关部分:

protected override async Task<HttpResponseMessage>
    SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
  Trace.TraceInformation("Going to validate client certificate.");

  var x509Certificate2 = request.GetClientCertificate();
  Trace.TraceInformation("Client cert: {0}", x509Certificate2 == null
    ? "<null>"
    : x509Certificate2.Subject);

  try
  {
    var headerKeys = string.Join("|", request.Headers.Select(h => h.Key));
    Trace.TraceInformation("Header keys: {0}", headerKeys);
  ...
Run Code Online (Sandbox Code Playgroud)

以及结果日志输出:

2015-12-07T08:08:24  PID[8464] Information Going to validate client certificate.
2015-12-07T08:08:24  PID[8464] Information Client cert: <null>
2015-12-07T08:08:24  PID[8464] Information Header keys:
  Connection|Host|Max-Forwards|Conf-Organisation-Key|Ocp-Apim-Subscription-Key|
  X-Forwarded-For|X-LiveUpgrade|X-ARR-LOG-ID|DISGUISED-HOST|X-SITE-DEPLOYMENT-ID|
  X-Original-URL
Run Code Online (Sandbox Code Playgroud)

因此,没有客户端证书,也没有X-ARR-ClientCert标头。

更新3:这是当我直接使用客户端身份验证证书进入实际API时生成的日志:

2015-12-07T09:16:45  PID[8464] Information Going to validate client certificate.
2015-12-07T09:16:45  PID[8464] Information Client cert: CN=rwildenberg@itq.nl
2015-12-07T09:16:45  PID[8464] Information Header keys:
  Connection|Accept|Accept-Encoding|Accept-Language|Cookie|Host|Max-Forwards|
  User-Agent|Upgrade-Insecure-Requests|DNT|X-LiveUpgrade|X-ARR-LOG-ID|DISGUISED-HOST|
  X-SITE-DEPLOYMENT-ID|X-Original-URL|X-Forwarded-For|X-ARR-SSL|X-ARR-ClientCert
Run Code Online (Sandbox Code Playgroud)

直接来自请求的客户端证书和预期的X-ARR-ClientCert标头。

更新4:最后,这碰巧是我自己的错误(当然)。我确信后端的url https实际上是http。客户端证书身份验证仅在https上有效,因此事后看来,在后端没有找到证书是很合理的……

小智 5

您还需要为 Azure 托管 API 启用客户端证书身份验证 - 此步骤将确保 Azure 继承传入请求中的任何 X-ARR-ClientCert 标头。

否则,Azure 会在 Request.Headers 到达您的 API 之前从 Request.Headers 中删除 X-ARR-ClientCert 标头。

注意:此设置仅适用于付费订阅

  1. 转到https://resources.azure.com/ 并选择所需的 azure 帐户
  2. 选择订阅 -> 资源组 -> your_resource_group -> 提供商 -> microsoft.web -> 站点 -> your_website
  3. 从顶部按钮进入读/写模式,进入编辑模式,然后在属性下设置属性“clientCertEnabled”:true。
"properties": {
    "name": "my-site",
    "state": "Running",
    "hostNames": [
      "my-site.azurewebsites.net",
      "(string)"
    ],
    ...
    "clientCertEnabled": true,
    ...
 }
Run Code Online (Sandbox Code Playgroud)

文档:https://learn.microsoft.com/en-us/azure/app-service-web/app-service-web-configure-tls-mutual-auth


Dar*_*ler 2

您真的想管理 API 的所有用户的客户端证书吗?我了解使用客户端证书来确保只有 APIM 可以直接与您的后端 API 对话。通常,通过 API 管理公开 API 的开发人员使用 API 密钥来控制对 API 的访问。使用这种方法可以根据不同的配置“产品”应用策略。

如果您拥有管理证书创建和撤销的基础设施,那么这可能是正确的选择,这只是一种不常见的方法。话虽如此,我将研究 API 管理中有哪些可用选项能够提取 APIM 网关上的证书指纹。