asp.net - 通过 WSS 连接时无法创建 SSL/TLS 安全通道,仅在 Azure 上

标签 asp.net ssl asp.net-web-api websocket azure-web-app-service

我正在构建一个 Web Api(使用 ASP.NET Web API),它通过安全 WebSockets 连接到我们的客户端公开的端点(wss://client-domain:4747/app/engineData).他们以 .pem 格式(root.pemclient.pem)给了我他们的证书,以及一个私钥(client_key.pem) .

为了完成这项工作,我做了以下工作:

1) 将 client.pemclient_key.pem 转换为单个 .pfx 文件(此处使用:Convert a CERT/PEM certificate to a PFX certificate)

2) 我使用了库System.Net.WebSockets,并编写了以下代码:

private void InitWebSockesClient()
{
    client = new ClientWebSocket();
    client.Options.SetRequestHeader(HEADER_KEY, HEADER_VALUE); //Some headers I need
    AddCertificatesSecurity();
}

private void AddCertificatesSecurity()
{
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
         | SecurityProtocolType.Tls11
         | SecurityProtocolType.Tls12;

     // I KNOW THIS SHOULDNT BE USED ON PROD, had to use it to make it 
     // work locally.
     ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

      X509Certificate2 x509 = new X509Certificate2();
      // this is the pfx I converted from client.pem and client_key
      byte[] rawData = ReadFile(certificatesPath + @"\cert.pfx");
      x509.Import(rawData, "123456", X509KeyStorageFlags.UserKeySet);

      X509Certificate2Collection certificateCollection = new X509Certificate2Collection(x509);

      client.Options.ClientCertificates = certificateCollection;
 }

当我想连接时,我会调用:

 public async Task<bool> Connect()
 {
    Uri uriToConnect = new Uri(URL);
    await client.ConnectAsync(uriToConnect, CancellationToken.None);
    return client.State == WebSocketState.Open;
 }

这在本地运行良好。但是每当我在 Azure(应用程序服务)上部署我的 Web Api 并向它发出 HTTP 请求时,它都会抛出:

System.Net.WebSockets.WebSocketException - Unable to connect to the remote server.

内部异常:

System.Net.WebException - The request was aborted: Could not create SSL/TLS secure channel.

我在 AppService 实例上启用了 WebSockets。

如果我删除始终为证书验证返回 true 的行,它甚至在本地也不起作用,并且消息如下所示:

The remote certificate is invalid according to the validation procedure.

所以肯定我的证书有问题,这三个 .pem 文件现在正在 node.js 中类似的 [![enter image description here][1]][1]app 中使用并且工作正常,WSS 连接已正确建立。我真的不知道每个人的用法,所以我有点迷路了。

这些是我要连接的域的密码套件:/image/ZFbo3.png

最佳答案

受 Tom 评论的启发,我最终通过将证书添加到 Azure App Service 中的 Web 应用程序而不是尝试从文件系统中使用它来实现它。首先,我在 Azure 的 SSL 证书部分上传了 .pfx 文件。然后,在 App 设置中,我添加了一个名为 WEBSITE_LOAD_CERTIFICATES 的设置,其中包含我想要的证书的指纹(.pfx)。

在那之后,我修改了我的代码来完成这样的工作:

private void InitWebSockesClient()
{
     client = new ClientWebSocket();
     client.Options.SetRequestHeader(HEADER_KEY, HEADER_VALUE); //Some headers I need
     AddCertificateToWebSocketsClient();
}

private void AddCertificateToWebSocketsClient()
{
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
                        | SecurityProtocolType.Tls12;

     // this should really validate the cert
     ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

    // reading cert from store
    X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    certStore.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection certCollection = 
           certStore.Certificates.Find(X509FindType.FindByThumbprint,
                            CERTIFICATES_THUMBPRINT,
                            false);

     if (certCollection.Count > 0)
     {
         client.Options.ClientCertificates = certCollection;
     }
     else
     {
         // handle error
     }
     certStore.Close();
}

其中 CERTIFICATES_THUMBPRINT 是一个字符串(您的证书的指纹,即您在 Azure 上看到的那个)。

如果你想让它在本地工作,你只需要在你的计算机上安装证书,否则它不会在商店中明显找到它。

Azure 文档中所有这些的引用:https://learn.microsoft.com/en-us/azure/app-service/app-service-web-ssl-cert-load .

关于asp.net - 通过 WSS 连接时无法创建 SSL/TLS 安全通道,仅在 Azure 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48407899/

相关文章:

c# - 根据按月过滤的两个日期值获取数据

c# - 如何使用 C# 处理 401 错误响应?

ssl - 如果 header 存在,Nginx 设置 proxy_set_header

C# 测试文件上传

c# - ASP.net WebApi 新注册的路由显示,该请求无效

ASP.Net FormsAuthentication Redirect 丢失 Redirect 和 Application_AuthenticateRequest 之间的 cookie

asp.net - 如何在 .net 3.5 中托管我的 MVC3 应用程序?

apache - 中间证书和根证书文件

iphone - 将 HTTPS 与 Monotouch 和 WCF 结合使用

c# - 如何读取 Response 主体并将其更改为 Controller 的覆盖 OnActionExecuted 方法