【问题标题】:Azure APIM URL throws System.Net.WebException - How SSL/TLS making difference in azure web api and azure APIM?Azure APIM URL 引发 System.Net.WebException - SSL/TLS 如何在 azure web api 和 azure APIM 中发挥作用?
【发布时间】:2020-01-02 11:46:45
【问题描述】:

我创建了web api 并尝试使用c# 发出GET 请求,如下所示

namespace APIMCheck
{
 class Program
 {
    static void Main(string[] args)
    {
        string thumbprint = "***"; 
        string url @"https://******-us-stats-webapi.azurewebsites.net/statistics/v1/masterData/carTypes";


        X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);

        X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, true);
        X509Certificate2 certificate = certificates[0];
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);


        req.ClientCertificates.Add(certificate);
        req.Method = WebRequestMethods.Http.Get;

        Console.WriteLine(Program.CallAPI(req).ToString());

        Console.Read();

    }

    public static string CallAPI(HttpWebRequest req)
    {
        var httpResponse = (HttpWebResponse)req.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            return streamReader.ReadToEnd();
        }
    }

    public static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }
  }
}

我得到了数据的响应。都很好。

现在,我创建了Azure APIM,它将作为上述web API 的前端

这是在 Azure API 管理门户中配置的策略

<policies>
  <inbound>
    <base />
      <choose>
        <when condition="@(context.Request.Certificate.Verify() != true || context.Request.Certificate == null || context.Request.Certificate.Issuer != "CN=MySubCA, DC=MYEXT, DC=NET" || context.Request.Certificate.NotAfter < DateTime.Now)">
            <return-response>
                <set-status code="403" reason="Invalid client certificate" />
                <set-body template="none">@(context.Request.Certificate.Issuer.ToString())</set-body>
            </return-response>
        </when>
    </choose>
    </inbound>
    <backend>
     <base />
    </backend>
    <outbound>
     <base />
    </outbound>
    <on-error>
     <base />
    </on-error>
</policies>

现在,将url 更改为指向apim

 string url = @"https://******-us-stats-apim.azure-api.net/statistics/v1/masterData/carTypes";

我得到以下错误

请求被中止:无法为 HttpWebRequest 创建 SSL/TLS 安全通道

SSL/TLS 如何在 web api 和 APIM 中发挥作用?

与防火墙有关吗?

【问题讨论】:

    标签: c# azure ssl asp.net-web-api azure-api-management


    【解决方案1】:

    默认情况下,为 Azure API 管理网关启用 TLS 1.2

    你可以去你的azure api管理(在门户上)>Protocol settings>打开tls 1.2ssl 3.0

    【讨论】:

    • 我可以开启 SSL 3.0 但 TLS 1.2 被禁用
    • TLS 1.2 被禁用? TLS 1.1 怎么样?
    【解决方案2】:

    您可以在测试应用中执行一些操作,而 APIM 无法单独完成:

    1. 启用 SSL3
    2. 添加客户端证书
    3. 忽略任何 SSL 验证错误。

    在上述内容中,只有 #2 可以在生产中使用。 SSL3 已弃用,不应在生产中使用。忽略任何 SSL 错误也是不明智的,因为这样您就无法确定您正在与您的服务器通信。

    现在假设您对以上所有内容都满意:

    1. 对于 SSL3,请遵循 @Joey Cai 的回答。
    2. 要允许 APIM 使用您的证书对后端进行身份验证,请遵循以下指南:https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-mutual-certificates
    3. 要在 APIM 级别控制 SSL 验证,请使用以下 REST API:https://docs.microsoft.com/en-us/rest/api/apimanagement/2019-01-01/backend/createorupdateset-backend-service 策略。

    #2 和#3 的主要原因是客户端到 APIM 和 APIM 到后端是两个独立的连接。因此,当 APIM 需要调用后端时,它必须有可用的客户端证书(如果后端需要)。这也意味着默认情况下 APIM 不会要求客户端提供证书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2022-08-15
      • 2022-01-02
      • 2022-07-25
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多