如何使用SSL加密并用C#实现SignalR的客户端和服务器端通信

问题描述 投票:0回答:1

我有一个使用C#.Net Framework实现的SignalR客户端和服务端的基本连接代码如下: 客户

using Microsoft.AspNet.SignalR.Client;
namespace WinFormsClient
{
    public partial class FrmClient : Form
    {
        //Connection to a SignalR server
        HubConnection _signalRConnection;

        //Proxy object for a hub hosted on the SignalR server
        IHubProxy _hubProxy;
        private async Task connectAsync()
        {
            //Set Certificate callback
            ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
            //Create a connection for the SignalR server
            _signalRConnection = new HubConnection("https://10.224.10.10:12316/signalr/SimpleHub/");
            _signalRConnection.StateChanged += HubConnection_StateChanged;
            _signalRConnection.Closed += HubConnection_Closed;

            //Get a proxy object that will be used to interact with the specific hub on the server
            //Ther may be many hubs hosted on the server, so provide the type name for the hub
            _hubProxy = _signalRConnection.CreateHubProxy("SimpleHub");

            //Reigster to the "AddMessage" callback method of the hub
            //This method is invoked by the hub
            _hubProxy.On<string, string>("AddMessage", (name, message) => writeToLog($"{name}:{message}"));            

            try
            {
                //Connect to the server
                await _signalRConnection.Start();

                //Send user name for this client, so we won't need to send it with every message
                await _hubProxy.Invoke("SetUserName", txtUserName.Text);
            }
            catch (Exception ex)
            {
                writeToLog($"Error:{ex.Message}");
            }
        }
        private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErros)
        {
            return true;
        }
    }
}

服务器

using Microsoft.Owin.Hosting;
using Microsoft.AspNet.SignalR;
using System.ComponentModel;

namespace WinFormsServer
{
    public partial class FrmServer : Form
    {
        private IDisposable _signalR;
        private void btnStartServer_Click(object sender, EventArgs e)
        {
            try
            {
                //Start SignalR server with the give URL address
                //Final server address will be "URL/signalr"
                //Startup.Configuration is called automatically
                _signalR = WebApp.Start<Startup>("https://localhost:12316");
                writeToLog($"Server started at:{txtUrl.Text}");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

using Owin;
using Microsoft.Owin.Cors;

namespace WinFormsServer
{
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            //CORS need to be enabled for calling SignalR service 
            app.UseCors(CorsOptions.AllowAll);
            //Find and reigster SignalR hubs
            app.MapSignalR();
        }
    }
}

现在,我将客户端和服务器部署在同一台机器上,并通过以下语句将自签名SSL证书绑定到指定的IP和端口。

Netsh http add sslcert ipport=10.224.10.10:12316 certificate=xxxx appid={xxxx}

然后我尝试启动服务器,它运行正常。 当我尝试启动客户端连接服务器时,提示如下错误:

StatusCode:400, ReasonPhrase:'Bad Request', Version: 1.1, Content: System.Net.Http.SteamContent, Headers:
{
    Connection: close
    Data: Mon, 20 Nov 2023 04:41:15 GMT
    Server: Microsoft-HTTPAPI/2.0
    Content-Length:334
    Content-Type:text/html; charset=us-ascii
}

我已将自签名证书导入到 MMS 控制台根的个人和受信任的根证书颁发机构以及本地计算机和当前用户中。 不知道为什么客户端无法连接到服务器。有人可以帮助我吗?

另外,如果我将服务器端URL改为 https://10.224.10.10:12316/ ,会提示服务器启动失败,Inner Exception HttpListenerException: Access is Denied。 我在本地入站规则中添加了服务名称和绑定端口12316。 我还需要配置什么吗?

c# ssl signalr
1个回答
0
投票

我找到了解决办法。 通过以管理员身份运行服务器端,我可以使用公共 IP 来启动 SignalR 服务器。

我有一个新问题。 我已配置入站规则,但无法从其他客户端计算机访问 SignalR 服务器。客户端使用以下 url 来请求连接。 https://公网IP:12312/signalr/SimpleHub/

© www.soinside.com 2019 - 2024. All rights reserved.