1. 程式人生 > >SignalR具有自簽名SSL和自主機

SignalR具有自簽名SSL和自主機

SignalR具有自簽名SSL和自主機

 

在研究中試過我的運氣,但到目前為止還沒有快樂。

我想將SignalR javascript客戶端連線到自簽名的SignalR Windows服務繫結到自簽名SSL證書。

我的應用程式在http上工作得很好,但是當Owin WebApplication開始使用https時,客戶端會重複斷開連線。

以下是我使用SSL配置SignalR所做的工作。

  1. 使用IIS建立自簽名證書
  2. 將證書匯入mmc中的受信任的根證書頒發機構(不確定是否有幫助)
  3. Ran NETSH命令將SSL繫結到埠8080

    netsh http add sslcert ipport=0.0.0.0:8080 certhash=123456f6790a35f4b017b55d09e28f7ebe001bd appid={12345678-db90-4b66-8b01-88f7af2e36bf}
    
  4. 在自託管HubConnection例項中添加了程式碼以新增這樣的匯出SSL(儘管這應該無關緊要,因為它是無法連線的客戶端):

    if (File.Exists("MyCert.cer") 
        && Settings.GetSetting(Settings.Setting.SrProtocol).Equals("https", StringComparison.InvariantCultureIgnoreCase))
            connection.AddClientCertificate(X509Certificate.CreateFromCertFile("MyCert.cer"));
    
  5. 使用https啟動Owin WebApplication(這應該在http.sys中建立繫結)

    string registerUrl = string.Format("{0}://SOME.WHERE.COM:{1}", Service.Server.SrProtocol, Service.Server.SrPort);
    WebApp.Start<StartUp>(registerUrl);
    

在SignalR 2.0文件中,它說:

要啟動Web伺服器,請呼叫WebApplication.Start(endpoint)。

您現在應該可以在瀏覽器中導航到端點/訊號器/集線器。

當我瀏覽到URL http://SOME.WHERE.COM:8080/signalr/hubs時,我成功接收到驅動SignalR的javascript。

當我瀏覽到URL https://SOME.WHERE.COM:8080/signalr/hubs時,我沒有成功,我收到“使用FF重置了與伺服器的連線”。

我考慮了一些額外的觀點:

  • NETSH SHOW表示網址已註冊

    URL group ID: E300000240000022
    State: Active
    Request queue name: Request queue is unnamed.
    Properties:
        Max bandwidth: inherited
        Max connections: inherited
        Timeouts:
            Timeout values inherited
        Number of registered URLs: 1
        Registered URLs: HTTPS://SOME.WHERE.COM:8080/
    
  • NETSH SHOW表示SSL證書繫結到8080:

    IP:port                 : 0.0.0.0:8080 
    Certificate Hash        : 123456f6790a35f4b017b55d09e28f7ebe001bd
    Application ID          : {12345678-db90-4b66-8b01-88f7af2e36bf} 
    Certificate Store Name  : (null) 
    Verify Client Certificate Revocation    : Enabled
    Verify Revocation Using Cached Client Certificate Only    : Disabled
    Usage Check    : Enabled
    Revocation Freshness Time : 0 
    URL Retrieval Timeout   : 0 
    Ctl Identifier          : (null) 
    Ctl Store Name          : (null) 
    DS Mapper Usage    : Disabled
    Negotiate Client Certificate    : Disabled
    

任何幫助是極大的讚賞!

  分享 改善這個問題 於2013年8月9日15:50 問 ISZ 547823 新增評論

1答案

活躍的 最老 投票 15投票

我相信它現在對我有用。這是我為了讓事情流動而採取的步驟:

SSL注意事項

SSL和SignalR(Owin WebApplication)要求將證書繫結到埠。

  1. 使用IIS生成自簽名證書,這應該將證書放入CERTMGR中的LOCAL COMPUTER> Personal> Certificates資料夾
  2. 在CERTMGR中移位+拖動證書到LOCAL COMPUTER>受信任的根證書頒發機構>證書資料夾,它應該在那裡複製它
  3. 執行以下命令,將SSL證書繫結到0.0.0.0:8080

    netsh http add sslcert ipport=0.0.0.0:8080 certhash=123456f6790a35f4b017b55d09e28f7ebe001bd appid={12345678-db90-4b66-8b01-88f7af2e36bf} 
    netsh http show urlacl > D:\urlacl.txt
    

    輸出:

    Reserved URL            : https://*:8080/ 
    User: SOMEWHERE\Administrator
    Listen: Yes
    Delegate: No
    SDDL: D:(A;;GX;;;S-1-5-21-138209071-46972887-2260295844-1106) 
    
  4. 執行以下NETSH命令,將埠8080的所有IP地址保留為“我的服務”應用程式ID和服務帳戶

    netsh http add urlacl url=https://*:8080/ user=SOMEWHERE\Administrator listen=yes
    netsh http show sslcert > D:\sslcert.txt
    

    輸出:

    IP:port                 : 0.0.0.0:8080 
    Certificate Hash        : 123456f6790a35f4b017b55d09e28f7ebe001bd
    Application ID          : {12345678-db90-4b66-8b01-88f7af2e36bf} 
    Certificate Store Name  : (null) 
    Verify Client Certificate Revocation    : Enabled
    Verify Revocation Using Cached Client Certificate Only    : Disabled
    Usage Check    : Enabled
    Revocation Freshness Time : 0 
    URL Retrieval Timeout   : 0 
    Ctl Identifier          : (null) 
    Ctl Store Name          : (null) 
    DS Mapper Usage    : Disabled
    Negotiate Client Certificate    : Disabled
    
  5. 更新MyServices.exe.config檔案以使用https協議(這些是appSetting鍵,用於在My Service啟動時動態設定SignalR的協議和埠)

    <add key="SrProtocol" value="https" />
    <add key="SrPort" value="8080" />
    
  6. 使用NETSTAT START命令啟動My Service

  7. 執行以下NETSH命令以顯示服務狀態佔用已註冊的URL

    netsh http show servicestate > D:\servicestate.txt
    

    輸出:

    Server session ID: C300000320000039
    Version: 2.0
    State: Active
    Properties:
        Max bandwidth: 4294967295
        Timeouts:
            Entity body timeout (secs): 120
            Drain entity body timeout (secs): 120
            Request queue timeout (secs): 120
            Idle connection timeout (secs): 120
            Header wait timeout (secs): 120
            Minimum send rate (bytes/sec): 150
    URL groups:
    URL group ID: C600000340000138
        State: Active
        Request queue name: Request queue is unnamed.
        Properties:
            Max bandwidth: inherited
            Max connections: inherited
            Timeouts:
                Timeout values inherited
            Number of registered URLs: 1
            Registered URLs:
                HTTPS://*:8080/
    

我的應用程式不依賴於IIS,但是一旦我使用IIS臨時建立繫結到我的SSL證書的埠,我的應用程式就開始工作了,我能夠檢查NETSH服務狀態以檢視IIS是如何做到的。我已經刪除了IIS繫結並運行了安裝說明,但仍然取得了成功。

我的Owing初創公司看起來像這樣:

private void configureMessaging()
{
    string registerUrl = string.Format("{0}://*:{1}", Service.Server.SrProtocol, Service.Server.SrPort);

    try
    {
#if DEBUG
        //System.Diagnostics.Debugger.Launch();
#endif
        //  Starts an owin web application to host SignalR, using the protocol and port defined.
        WebApp.Start<StartUp>(registerUrl);
    }
    catch (Exception ex)
    {
        Logger.Logs.Log(string.Format("Failed to configure messaging.  Exception: {0}", ex.RecurseInnerException()), LogType.Error);            

        if (ex is HttpListenerException || ex.InnerException is HttpListenerException)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "netsh.exe";
                p.StartInfo.Arguments = string.Format("netsh http delete urlacl url={0}"
                    , registerUrl
                    );
                p.Start();
                p.StandardOutput.ReadToEnd();
                p.WaitForExit();
            }
            catch (Exception exP)
            {
                Logger.Logs.Log(string.Format("Failed to delete urlacl {0}.  Exception: {1}"
                    , registerUrl
                    , exP.RecurseInnerException()
                    )
                    , LogType.Error
                    )
                    ;

                retries = 5;
            }
        }

    if (retries < 5)
    {
        retries++;

        Logger.Logs.Log(string.Format("Attempting to configure messaging again.  Attempt No. {0}", retries), LogType.Warn);

        Thread.Sleep(1000);

        configureMessaging();
    }
    else
        Logger.Logs.Log(string.Format("Exceeded total number of retries to configure messaging.", retries), LogType.Error);

    }

}

自託管的HubConnetion例項如下所示:

    public IHubProxy MyHubProxy
    {
        get
        {
            if (this._MyHubProxy == null)
            {
                var connection = new HubConnection(string.Format("{0}://{1}:{2}/"
                    , Settings.GetSetting(Settings.Setting.SrProtocol)
                    , MyHub.GetLocalhostFqdn(null)
                    , Settings.GetSetting(Settings.Setting.SrPort)
                    )
                    )
                    ;
                this._MyHubProxy = connection.CreateHubProxy("MyHub");

                if (File.Exists("My.cer")
                    && Settings.GetSetting(Settings.Setting.SrProtocol).Equals("https", StringComparison.InvariantCultureIgnoreCase))
                    connection.AddClientCertificate(X509Certificate.CreateFromCertFile("My.cer"));

                connection.Start().Wait();
            }

            return this._MyHubProxy;
        }
    }

這裡有一些程式碼比相關的更多,但希望它可能有所幫助!

分享 改善這個答案 於2017年4月11日13:47編輯 心教堂 17.3k9102142 於2013年8月10日18:16 回答 ISZ 547823
  •   “我的應用程式不依賴於IIS,但是一旦我使用IIS臨時建立繫結到我的SSL證書的埠,我的應用程式就開始工作了,我能夠檢查NETSH服務狀態,看看IIS是如何做到的。我從那以後刪除了IIS繫結並運行了安裝說明,但仍然取得了成功。“ 所以你說有人需要IIS首先設定ssl證書然後ssl在Owin中使用自託管工作嗎?那將是愚蠢的,因為Owin應該與任何網路伺服器脫鉤......你會在5個月後將你的答案標記為解決方案,現在有更多的經驗嗎? -  Pascal 2014年 1月26日13:38
  •   是的,我不是建議IIS成為這個過程的一部分,它只是我試圖找到它的底部。此外,我計劃在我的下一個版本中回到SSL,並且很樂意在我稽核時將其標記為解決方案,請耐心等待;)謝謝! -  ISZ 2014年 2月6日