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

r1zhe5dt  于 12个月前  发布在  C#
关注(0)|答案(1)|浏览(267)

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

客户端

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/,会提示服务器启动失败,内部异常HttpListenerException:Access is denied。我在本地的服务器规则中添加了服务名称和绑定端口12316,还需要配置什么吗?

11-21-2023最新更新

我已经找到了一个解决方案。通过以管理员身份运行服务器端,我可以使用公共IP启动SignalR服务器。
我有一个新问题。我已经配置了SignalR规则,但我无法从其他客户端计算机访问SignalR服务器。客户端使用以下URL请求连接。https://public IP:12316/signalr/SimpleHub/
错误响应为发送请求时发生错误。SocketException:连接尝试失败,因为连接的一段时间后没有正确响应,或者连接的主机没有响应,建立连接失败10.224.10.10:12316

i1icjdpr

i1icjdpr1#

我已经找到了一个解决方案。通过以管理员身份运行服务器端,我可以使用公共IP启动SignalR服务器。

相关问题