本文介绍了SignalR-无法为SSL/TLS安全通道建立信任关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器代码中调用我的signalR集线器方法,并给我一个错误

I'm trying to Invoke my signalR hub method from server code and its giving me an error

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

my server side code is something like this

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
    }
}

在集线器类中,我有一个方法,例如SayHello(),该方法向所有用户发送消息.

In hub class I have one method, called for example SayHello(), which sending message to all users.

使用http可以正常工作,但是当我从IIS绑定中删除80端口并仅保留https端口时,每次尝试集线器连接时,它每次都会给我错误.我尝试了许多通过搜索找到的内容,但没有找到这些作品.还有其他人有类似的问题吗,请帮忙.

With http its working perfectly, but when I remove 80 port from my IIS bindings, and leave only https port, it giving me error every time, when trying to Start() hub connection. I tried many things that I found by searching, but non of these works.Does anyone else had similar problems, please help.

推荐答案

好,我做了类似的事情

 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对话中手动对其进行了验证(实际上是通过).我不知道这种解决方案有多少合法性,但目前为止.

by this, I found my certificate and manually validate(actually pass through) it in a SSL conversation. I don't know how much legit is this solution, but for now it is.

我更改了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;
}

通过打开证书链(最后一个元素)中的指纹参数,我将打开证书存储库,从中查找我的物品.

by this I'm opening certificate store, finding my item from it, by passing thumbprint parameter from certificate chain (last element).

这篇关于SignalR-无法为SSL/TLS安全通道建立信任关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:43