【问题标题】:Azure Functions - Configure client certificate authenticationAzure Functions - 配置客户端证书身份验证
【发布时间】:2018-09-16 02:23:39
【问题描述】:

函数是否支持在消费计划中使用客户端证书授权访问函数?类似于here 描述的方法?基本上,如果调用者没有提供有效的客户端证书,我正在寻找 Functions 运行时立即拒绝连接请求,而无需在代码中实现该授权例程。

【问题讨论】:

  • 你可以试一试告诉我们:-)
  • 我们确实支持。是的,请尝试告诉我们。
  • @SuwatCh 启用此设置后,我在访问功能门户刀片时遇到问题。一切正常,直到我切换客户端证书设置 - 然后功能运行时无法启动并且门户功能刀片错误。有什么想法吗?
  • @SuwatCh 我也有这个问题。一旦我打开客户端证书设置,对我的函数的任何请求都会失败,并出现 HTTP 401 错误。我正在尝试使用自签名证书。我需要有一个由 CA 签名的证书吗?

标签: azure azure-functions azure-functions-runtime


【解决方案1】:

根据您的要求,我创建了我的 C# HttpTrigger 函数来检查这个问题,这里是核心代码:

if(req.Headers.Contains("X-ARR-ClientCert")) 
{   
    byte[] clientCertBytes = Convert.FromBase64String(req.Headers.GetValues("X-ARR-ClientCert").FirstOrDefault());
    var clientCert = new X509Certificate2(clientCertBytes);
    return req.CreateResponse(HttpStatusCode.OK,"Thumbprint: "+clientCert.Thumbprint);
}
return req.CreateResponse(HttpStatusCode.OK, "Hello world");

对于应用服务计划,该功能可以如下工作:

根据我的测试,该功能在消费计划下也可以按预期工作。

您可以关注How To Configure TLS Mutual Authentication for Web App 或直接登录 Azure 门户并转到您的函数应用,单击平台功能选项卡下的“网络 > SSL”,然后启用传入客户端证书选项。

【讨论】:

    【解决方案2】:

    这是我想出的代码,注意:这是 Azure Functions v1,当 req 是 HttpRequestMessage

    来电者:

    X509Certificate2 clientCert = req.GetClientCertificate();
    
    if (!IsValidClientCertificate(clientCert))
    {
        return req.CreateErrorResponse(HttpStatusCode.Unauthorized, "A valid client certificate is not found");
    }
    

    对于 Azure Functions v2,您可以使用 req.HttpContext.Connection.ClientCertificateHttpRequest 获取客户端证书

    基本验证功能:

    static bool IsValidClientCertificate(X509Certificate2 clientCert)
    {
        // check the cert's thumbprint against expected thumbprint
        if (clientCert.Thumbprint != "<expected thumprint>"
        { 
            return false;
        }
    
        // check that we're within the cert's validity period
        if (DateTime.Now > clientCert.NotAfter || DateTime.Now < clientCert.NotBefore)
        {
            return false;
        }
    
        // optionally check cert chaining validity
        // if(!clientCert.Verify()) { return false; }
    }
    

    【讨论】:

    • HttpRequest 好像没有GetClientCertificate() 方法?
    • 您在 Azure Function 方法中获得的参数是 HttpRequestMessage 类型,它具有该方法。见docs.microsoft.com/en-us/sandbox/functions-recipes/…
    • 嗯,是的,我记得它曾经是,但现在你默认得到一个HttpRequest。我似乎在不久前看到了一个关于这个的 Github 问题,一位 MS 员工说这是他们现在的建议。我发现我可以使用req.HttpContext.Connection.ClientCertificate 获取客户端证书
    • 啊,你是对的,在 v1 中你得到一个 HttpRequestMessage,在 v2 中你得到一个 HttpRequest。关于如何获得客户证书的好发现!我会更新我的答案
    【解决方案3】:

    是的。如果我理解正确,您想用 403 拒绝任何没有客户端证书的 https 请求

    这是使用 Azure CLI 启用它的方法

    az webapp update --set clientCertEnabled=true --name <app_name> --resource-group <group_name>
    

    微软文档here

    您也可以在 Azure 门户中执行此操作,在 Azure Function App => 配置 => 常规设置下

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 2011-04-09
      • 2013-10-07
      • 1970-01-01
      • 2022-06-23
      • 2018-02-12
      • 2012-09-01
      相关资源
      最近更新 更多