c# - SignalR - 无法为 SSL/TLS 安全通道建立信任关系

标签 c# ssl https signalr

我正在尝试从服务器代码调用我的 signalR hub 方法,但它给我一个错误

Could not establish trust relationship for the SSL/TLS secure channel.

我的服务器端代码是这样的

private void InvokeNotification(string methodName, params object[] args)
{
    try
    {
        string serverIp = ConfigurationManager.AppSettings["ServerIp"];
        var connection = new HubConnection(serverIp, useDefaultUrl: true);

        var myHub = connection.CreateHubProxy("myhub");
        connection.Start().Wait();
        myHub.Invoke(methodName, args);
    }
    catch (Exception ex)
    {
        //Some error handling
    }
}

在 hub 类中,我有一个方法,例如 SayHello(),它向所有用户发送消息。

使用 http 时它工作得很好,但是当我从我的 IIS 绑定(bind)中删除 80 端口并只保留 https 端口时,每次尝试 Start()< 时它都会给我错误 集线器连接。我尝试了很多通过搜索找到的东西,但这些都不起作用。 有没有其他人有类似的问题,请帮助。

最佳答案

好的,我做了这样的事情

 private void InvokeNotification(string methodName, params object[] args)
 {
     try
     {
         string serverIp = ConfigurationManager.AppSettings["ServerIp"];
         var connection = new HubConnection(serverIp, useDefaultUrl: true);
         var myHub = connection.CreateHubProxy("myhub");

         //This will ignore all certeficates
         //System.Net.ServicePointManager.ServerCertificateValidationCallback =
         //    ((sender, certificate, chain, sslPolicyErrors) => true);

         System.Net.ServicePointManager.ServerCertificateValidationCallback += 
                    new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
         connection.Start().Wait();
         myHub.Invoke(methodName, args);
     }
     catch (Exception ex)
     {
         //Some error handling
     }
  }

private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, 
                                    X509Chain chain, SslPolicyErrors policyErrors)
{
    bool result = false;
    if (cert.Subject.ToUpper().Contains("MY_CERT_ISSUER_NAME"))
    {
        result = true;
    }
    return result;
}

由此,我找到了我的证书并在 SSL 对话中手动验证(实际通过)它。我不知道这个解决方案有多少合法性,但目前是这样。

更新

我已经更改了 ValidateRemoteCertificate() 方法:

private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, 
                                    X509Chain chain, SslPolicyErrors policyErrors)
{
    bool result = false;
    X509Certificate2 cert2 = (X509Certificate2)cert;
    X509Store store = new X509Store(StoreName.Root);
    store.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection cc = store.Certificates.Find(X509FindType.FindByThumbprint, 
         chain.ChainElements[chain.ChainElements.Count - 1].Certificate.Thumbprint, true);
    store.Close();
    if (cc.Count > 0)
    {
        result = true;
    }
    return result;
}

由此我打开证书存储,通过从证书链(最后一个元素)传递指纹参数从中找到我的项目。

关于c# - SignalR - 无法为 SSL/TLS 安全通道建立信任关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433067/

相关文章:

xcode - SSL 使用 x.509 证书 cocoa

Certificate Authority 不在 JRE 时的 Java SSL 连接解决方​​案

Python 请求不适用于 https 代理

cakephp - 有选择地为 CakePHP 中的某些操作启用 SSL

c# - 根据范围数据查找值

c# - ASP :button onclick doesn't fire and page only refreshes

c# - 带有 Unity 的 Automapper - 在哪里放置 CreateMap 的东西?

spring - 仅为mongodb创建ssl连接

c# - 在*某些* WebAPI Controller 上禁用 SSL 客户端证书?

c# - 在什么时候我需要将查询工作卸载到数据库?