gpt4 book ai didi

ssl - 具有自签名 SSL 和自托管的 SignalR

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:30 26 4
gpt4 key购买 nike

在研究中尝试了我的运气,但到目前为止并不快乐。

我想将 SignalR javascript 客户端连接到绑定(bind)到自签名 SSL 证书的自托管 SignalR Windows 服务。

我的应用程序在 http 上运行良好,但当 Owin WebApplication 开始使用 https 时,客户端反复断开连接。

这是我为使用 SSL 配置 SignalR 所做的工作。

  1. 使用 IIS 创建了自签名证书
  2. 将证书导入到 mmc 中的受信任的根证书颁发机构(不确定是否有帮助)
  3. 运行 NETSH 命令将 SSL 绑定(bind)到端口 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 中创建绑定(bind))

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

在 SignalR 2.0 文档中,它说:

To start the web server, call WebApplication.Start(endpoint). You should now be able to navigate to endpoint/signalr/hubs in your browser.

当我浏览到 URL http://SOME.WHERE.COM:8080/signalr/hubs 时,我成功接收到驱动 SignalR 的 javascript。

当我浏览到 URL https://SOME.WHERE.COM:8080/signalr/hubs 时,我不成功,我使用 FF 收到“与服务器的连接已重置”。

我考虑过的一些额外要点:

  • NETSH SHOW表示该url已注册

    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 证书绑定(bind)到 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

非常感谢任何帮助!

最佳答案

我相信现在一切都对我有用。以下是我为使事情顺利进行而采取的步骤:

SSL 说明

SSL 和 SignalR (Owin WebApplication) 需要将证书绑定(bind)到端口。

  1. 使用 IIS 生成自签名证书,这应该将证书放入 CERTMGR 中的“本地计算机”>“个人”>“证书”文件夹中
  2. 在 CERTMGR 中 shift+拖动证书到本地计算机 > 受信任的根证书颁发机构 > 证书文件夹,应该在那里复制它
  3. 运行以下命令将SSL证书绑定(bind)到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 地址保留到 My Service 应用程序 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 协议(protocol)(这些是 appSetting 键,用于在我的服务启动时动态设置 SignalR 的协议(protocol)和端口)

    <add key="SrProtocol" value="https" />
    <add key="SrPort" value="8080" />
  6. 使用 NETSTAT START 命令启动我的服务

  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 临时创建绑定(bind)到我的 SSL 证书的端口,我的应用程序就开始工作了,并且我能够检查 NETSH 服务状态以了解 IIS 是如何做到的。从那以后,我放弃了 IIS 绑定(bind)并运行了安装说明,并且仍然成功。

我的 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;
}
}

这里的代码比相关的多一点,但希望它能有所帮助!

关于ssl - 具有自签名 SSL 和自托管的 SignalR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18150758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com