IIS 使用OpenSSL 生成的自签名证书,然后使用SingalR 客户端访问Https 站点通信
使用SignalR 的客戶端去發(fā)送消息給使用 https 部署的站點(diǎn),官方文檔目前并沒有詳細(xì)的教程,所以在此記錄下步驟:
?
使用管理員身份打開cmd 窗口,選擇一個(gè)整數(shù)保存文件夾的地址,切換到對(duì)應(yīng)的文件夾,然后執(zhí)行以下步驟:
(一) 生成證書文件
1. openssl genrsa -out test.key 1024
?
2. openssl req -new -x509 -key test.key -out test.cer -days 365 -subj /CN=10.158.229.20
注:CN=xxx 這里可以填寫部署網(wǎng)站的域名或者IP地址
?
3. openssl pkcs12 -export -out test.pfx -inkey test.key -in test.cer
注:生成了私鑰文件 test.pfx, 這一步需要輸入密碼,密碼會(huì)在導(dǎo)入IIS的時(shí)候使用
?
在以上步驟完成之后,生成如下幾個(gè)文件, test.cer, test.key, test.pfx:
4. 添加到證書管理的可信任證書節(jié)點(diǎn)中區(qū),這一步非常重要,如果不添加,就會(huì)導(dǎo)致SignalR無法正常訪問
4.1.? 運(yùn)行-->輸入mmc
4.2. File 中打開證書管理器
4.3. 在可信任根證書的節(jié)點(diǎn)右鍵導(dǎo)入證書
以上工作便完成了證書的創(chuàng)建和添加
?
(二)接下來開始部署IIS 站點(diǎn):
1. 在IIS 管理其中選擇 服務(wù)端證書
?
2. 選擇導(dǎo)入,找到對(duì)應(yīng)的 .pfx 證書,輸入密碼后,確認(rèn)導(dǎo)入
?
3. 在自己的站點(diǎn)綁定對(duì)應(yīng)的 SSL 證書
?
完成以上工作后,一個(gè)https 站點(diǎn)就已經(jīng)完成
?
測(cè)試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 站點(diǎn) 18 hubConnection = new HubConnection("https://10.158.229.20:443/"); 19 //var hubConnection = new HubConnection("http://10.158.229.20:8081/"); 20 //請(qǐng)求的時(shí)候一定帶上對(duì)應(yīng)站點(diǎn)部署的證書 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);//異步發(fā)送消息就好了 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 }服務(wù)端:參考的官方文檔
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 // 有關(guān)如何配置應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問 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>: ' + 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>
效果演示:
?
轉(zhuǎn)載于:https://www.cnblogs.com/yougmi/p/10477886.html
總結(jié)
以上是生活随笔為你收集整理的IIS 使用OpenSSL 生成的自签名证书,然后使用SingalR 客户端访问Https 站点通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电影编码JPEG2000与H.264
- 下一篇: elk的一些零碎知识