IIS 使用OpenSSL 生成的自簽名證書,而後使用SingalR 客戶端訪問Https 站點通訊

使用SignalR 的客戶端去發送消息給使用 https 部署的站點,官方文檔目前並無詳細的教程,因此在此記錄下步驟:javascript

 

使用管理員身份打開cmd 窗口,選擇一個整數保存文件夾的地址,切換到對應的文件夾,而後執行如下步驟:css

(一) 生成證書文件
1. openssl genrsa -out test.key 1024html

 

2. openssl req -new -x509 -key test.key -out test.cer -days 365 -subj /CN=10.158.229.20
注:CN=xxx 這裏能夠填寫部署網站的域名或者IP地址java

 

3. openssl pkcs12 -export -out test.pfx -inkey test.key -in test.cer
注:生成了私鑰文件 test.pfx, 這一步須要輸入密碼,密碼會在導入IIS的時候使用jquery

 

  在以上步驟完成以後,生成以下幾個文件, test.cer, test.key, test.pfx:app

4. 添加到證書管理的可信任證書節點中區,這一步很是重要,若是不添加,就會致使SignalR沒法正常訪問異步

  4.1.  運行-->輸入mmcasync

  4.2. File 中打開證書管理器測試

  4.3. 在可信任根證書的節點右鍵導入證書網站

  

  以上工做便完成了證書的建立和添加

 

(二)接下來開始部署IIS 站點:

  1. 在IIS 管理其中選擇 服務端證書

 

2. 選擇導入,找到對應的 .pfx 證書,輸入密碼後,確認導入

 

3. 在本身的站點綁定對應的 SSL 證書

 

  完成以上工做後,一個https 站點就已經完成

 

測試Demo:

  客戶端:    

 1 using Microsoft.AspNet.SignalR.Client;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Security.Cryptography.X509Certificates;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace SignalR_cli
10 {
11     class Program
12     {
13         static HubConnection hubConnection = null;
14         static IHubProxy proxy = null;
15         static void Main(string[] args)
16         {
17             //這裏鏈接使用OpenSSL 生成的證書部署的 https 站點
18             hubConnection = new HubConnection("https://10.158.229.20:443/");
19             //var hubConnection = new HubConnection("http://10.158.229.20:8081/");
20             //請求的時候必定帶上對應站點部署的證書
21             hubConnection.AddClientCertificate(X509Certificate.CreateFromSignedFile(@"E:\cer_demo\myself.cer"));
22             proxy = hubConnection.CreateHubProxy("ChatHub");
23             hubConnection.Start();
24             Console.WriteLine((int)hubConnection.State);
25             while (true)
26             {
27                 try
28                 {
29                     Console.WriteLine("Input:");
30                     var msg = Console.ReadLine();
31                     Go(msg);//異步發送消息就行了
32                 }
33                 catch (Exception e)
34                 {
35                     Console.WriteLine(e.Message);
36                 }
37             }
38         }
39         static async Task Go(string msg)
40         {
41             await proxy.Invoke("Send", "userCenter", msg);
42         }
43     }
44 }

  服務端:參考的官方文檔

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using Microsoft.AspNet.SignalR;
 6 
 7 namespace Web_SignalR
 8 {
 9     public class ChatHub : Hub
10     {
11         public void Send(string name, string message)
12         {
13             Clients.All.broadcastMessage(name, message);
14         }
15     }
16 }
 1 using System;
 2 using System.Threading.Tasks;
 3 using Microsoft.Owin;
 4 using Owin;
 5 
 6 [assembly: OwinStartup(typeof(Web_SignalR.Startup))]
 7 
 8 namespace Web_SignalR
 9 {
10     public class Startup
11     {
12         public void Configuration(IAppBuilder app)
13         {
14             // 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888
15             app.MapSignalR();
16         }
17     }
18 }
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>SignalR Simple Chat</title>
 5     <style type="text/css">
 6         .container {
 7             background-color: #99CCFF;
 8             border: thick solid #808080;
 9             padding: 20px;
10             margin: 20px;
11         }
12     </style>
13 </head>
14 <body>
15     <div class="container">
16         <input type="text" id="message" />
17         <input type="button" id="sendmessage" value="Send" />
18         <input type="hidden" id="displayname" />
19         <ul id="discussion"></ul>
20     </div>
21     <!--Script references. -->
22     <!--Reference the jQuery library. -->
23     <script src="Scripts/jquery-3.3.1.min.js"></script>
24     <!--Reference the SignalR library. -->
25     <script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
26     <!--Reference the autogenerated SignalR hub script. -->
27     <script src="signalr/hubs"></script>
28     <!--Add script to update the page and send messages.-->
29     <script type="text/javascript">
30         $(function () {
31             // Declare a proxy to reference the hub.
32             var chat = $.connection.chatHub;
33             // Create a function that the hub can call to broadcast messages.
34             chat.client.broadcastMessage = function (name, message) {
35                 // Html encode display name and message.
36                 var encodedName = $('<div />').text(name).html();
37                 var encodedMsg = $('<div />').text(message).html();
38                 // Add the message to the page.
39                 $('#discussion').append('<li><strong>' + encodedName
40                     + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
41             };
42             // Get the user name and store it to prepend to messages.
43             $('#displayname').val("test");
44             // Set initial focus to message input box.
45             $('#message').focus();
46             // Start the connection.
47             $.connection.hub.start().done(function () {
48                 $('#sendmessage').click(function () {
49                     // Call the Send method on the hub.
50                     chat.server.send($('#displayname').val(), $('#message').val());
51                     // Clear text box and reset focus for next comment.
52                     $('#message').val('').focus();
53                 });
54             });
55         });
56     </script>
57 </body>
58 </html>

  效果演示:

  

相關文章
相關標籤/搜索