Httplistener支持https

Wal*_*elt 59 .net httplistener x509certificate

似乎有很多令人困惑的,有时是冲突的信息,关于使.net HTTPListener https能够.我的理解如下:

  • 一个人的c#代码需要一个https前缀(例如https://*:8443),以便监听器理解它需要在此端口上为SSL请求提供服务.

  • 实际的SSL握手发生在幕后并由http.sys(安装在Windows机器上的某处)进行处理; C#代码不必显式管理ssl握手,因为它发生在封面下.

  • 需要在计算机上安装"x509可信证书" httpListener,并且某种程度上证书需要绑定到端口8443(在此示例中)

我的理解是否正确?如果没有,请教育我.

关于x509证书,我的理解是:

  • 使用makecert创建X509证书.此证书存储在个人存储中,需要转移到可信存储(这是http侦听器所在的位置).似乎我可以certMgr用来执行移动,或者我可以mmc用来实现移动.似乎还有超过1 X509证书格式(DER,Base64,pks,PSWD保护pks私有等)..有,我应该使用首选格式?

一旦我将证书带入可信商店,我就需要将它绑定到tcp端口.我在Win7上:我应该使用httpcfg还是netsh

任何提示/建议将不胜感激.

Wal*_*elt 81

我做了一堆功课,让这个工作.为.NET HttpListener添加SSL支持的步骤如下:

  1. 更新C#应用代码以包含https前缀.例

    String[] prefixes = { "http://*:8089/","https://*:8443/" };
    
    Run Code Online (Sandbox Code Playgroud)

    从代码方面来看就是这样.

  2. 对于证书方面的东西,使用Win SDK命令控制台(也可以使用VS Professional命令控制台)

    • 使用https创建的证书颁发机构.例:

      makecert -n "CN=vMargeCA" -r -sv vMargeCA.pvk vMargeCA.cer
      
      Run Code Online (Sandbox Code Playgroud)
    • 使用makecert.exe来创建一个SSL证书

      makecert.exe

    • 使用MMC GUI在Trusted Authority存储中安装CA.

    • 使用MMC GUI在个人存储中安装ssl证书
    • 将证书绑定到ip:端口和应用程序.例:

      makecert -sk vMargeSignedByCA -iv vMargeCA.pvk -n "CN=vMargeSignedByCA" -ic vMargeCA.cer vMargeSignedByCA.cer -sr localmachine -ss My

      certhash是来自ssl证书的指纹.你可以使用mmc找到这个appid在VS中找到...通常在assembly.cs中,寻找guid值

可能还有其他方法可以实现上述目标,但这对我有用.

  • 我注意到当我复制并粘贴到命令行时,有时会出现'?' 出现在"certhash ="和实际密钥之间.仔细检查你的输入. (4认同)
  • 有什么方法可以将根 CA 证书链接到中间证书? (2认同)

jpa*_*ugh 31

以下是我在Windows上设置独立服务器时使用的步骤,HTTPListener用于为HttpListener openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365应用程序创建自签名证书.如果您想进一步研究,它包含大量链接.

  1. localhostvia中创建一个独立服务器openssl pkcs12 -inkey bob_key.pem -in bob_cert.cert -export -out bob_pfx.pfx:

    var prefixes = {"http://localhost:8080/app/root", "https://localhost:8443/app/root"};
    var listener = new HttpListener();
    foreach (string s in prefixes)
        listener.Prefixes.Add(s);
    listener.Start();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建自签名证书:*

    1. makecert,它将提示您输入命令行上每个证书字段的值.对于通用名称,键入域名(例如certmgr.msc)
    2. mmc,以便可以使用其键在目标计算机上导入它.

    *有关替代方案的使用Certificates,请参阅Walter自己的答案.

  3. 打开本地计算机的证书管理器.当您运行时Computer Account,它会打开当前用户的证书管理器,这不是我们想要的.代替:

    1. 从目标计算机上的管理命令提示符处运行 Local Computer
    2. 按,或单击Ctrl + MFile > Add/Remove Snap-in
    3. 选择pfx,然后单击Add >
    4. 在出现的对话框中,选择mmc,然后单击Next
    5. 选择Personal.点击Finish,然后Okay
  4. 将证书(Personal Information Exchange)导入目标计算机上的Windows证书存储区

    1. All Files之前打开的窗口中,向下钻取Certificates (Local Computer) > Personal
    2. 右键单击Place all certificates in the following store,然后单击All Tasks -> Import...
    3. 在出现的对话框的第二个屏幕中,查找并导入您的证书.您必须将文件类型过滤器更改为PersonalTrusted Root Certification Authorities以便查找它
    4. 在下一个屏幕上,输入您在步骤2.1中选择的密码,并密切注意第一个复选框.这决定了您的证书的存储安全性,以及它的使用方便程度
    5. 在最后一个屏幕上,选择netsh.验证它是否显示httpcfg,然后单击Finish
    6. cmd.exe证书部分重复上面的导入过程.
  5. 为您的应用程序创建端口关联.在Windows Vista及更高版本中ipport,像我一样使用.(对于Windows XP及更早版本,请使用8443)

    • 在管理命令行中,键入以下内容以将SSL绑定*设置为您的应用程序以及相应的端口. 注意:这个命令很容易出错,因为(在PowerShell中)需要转义大括号.以下PowerShell命令将起作用:

      netsh http add sslcert ipport=0.0.0.0:8443 `
          certhash=110000000000003ed9cd0c315bbb6dc1c08da5e6 `
          appid=`{00112233-4455-6677-8899-AABBCCDDEEFF`}
      
      Run Code Online (Sandbox Code Playgroud)

      因为certhash,应使用以下内容:

      netsh http add sslcert ipport=0.0.0.0:8443 certhash=110000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
      
      Run Code Online (Sandbox Code Playgroud)
      • appid参数将导致ssl cert绑定到netsh每个网络接口上的端口; 要绑定到特定接口(仅限),请选择与该网络接口关联的IP地址.
      • HTTPListener只是证书指纹,删除了空格
      • HttpListener是存储在应用程序的程序集信息中的GUID.(旁注:openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365从这个问题及其答案判断,该机制显然是一个COM接口)

      *微软已经重定向的SSL绑定从链接这里那里.

  6. 启动你的网络服务器,你很高兴!

  • 在我的Windows上,生成.pfx文件的命令使用Git-(Bash)-for-Windows挂起.作为解决方案,只需根据[openssl-hangs-during-pkcs12-export]命令在命令前添加`winpty`(/sf/ask/2390985691/加载屏幕,进入随机状态). (4认同)

小智 7

以下命令为 localhost 生成 10 年的自签名证书,将其导入到本地计算机存储并在输出中显示指纹 (certhash):

powershell -Command "New-SelfSignedCertificate -DnsName localhost -CertStoreLocation cert:\LocalMachine\My -NotAfter (Get-Date).AddYears(10)"
Run Code Online (Sandbox Code Playgroud)

然后,您可以从输出中复制指纹并使用 netsh.exe 将证书附加到 localhost:443,例如:

netsh http add sslcert ipport=localhost:443 certhash=110000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
Run Code Online (Sandbox Code Playgroud)

适用于 Windows 8 或更高版本。需要管理员权限。


Tre*_*ess 6

我们可以使用 PowerShell 和 C# 导入证书(不需要手动步骤)。

详情见:https : //blog.davidchristiansen.com/2016/09/howto-create-self-signed-certificates-with-powershell/

我正在使用此代码:

/// <summary>
/// Create and install a self-signed certificate for HTTPS use
/// </summary>
private static void CreateInstallCert(int expDate, string password, string issuedBy)
{
    // Create/install certificate
    using (var powerShell = System.Management.Automation.PowerShell.Create())
    {
        var notAfter = DateTime.Now.AddYears(expDate).ToLongDateString();
        var assemPath = Assembly.GetCallingAssembly().Location;
        var fileInfo = new FileInfo(assemPath);
        var saveDir = Path.Combine(fileInfo.Directory.FullName, "CertDir");
        if (!Directory.Exists(saveDir))
        {
            Directory.CreateDirectory(saveDir);
        }

        // This adds certificate to Personal and Intermediate Certification Authority
        var rootAuthorityName = "My-RootAuthority";
        var rootFriendlyName = "My Root Authority";
        var rootAuthorityScript =
            $"$rootAuthority = New-SelfSignedCertificate" +
            $" -DnsName '{rootAuthorityName}'" +
            $" -NotAfter '{notAfter}'" +
            $" -CertStoreLocation cert:\\LocalMachine\\My" +
            $" -FriendlyName '{rootFriendlyName}'" +
            $" -KeyUsage DigitalSignature,CertSign";
        powerShell.AddScript(rootAuthorityScript);

        // Export CRT file
        var rootAuthorityCrtPath = Path.Combine(saveDir, "MyRootAuthority.crt");
        var exportAuthorityCrtScript =
            $"$rootAuthorityPath = 'cert:\\localMachine\\my\\' + $rootAuthority.thumbprint;" +
            $"Export-Certificate" +
            $" -Cert $rootAuthorityPath" +
            $" -FilePath {rootAuthorityCrtPath}";
        powerShell.AddScript(exportAuthorityCrtScript);

        // Export PFX file
        var rootAuthorityPfxPath = Path.Combine(saveDir, "MyRootAuthority.pfx");
        var exportAuthorityPfxScript =
            $"$pwd = ConvertTo-SecureString -String '{password}' -Force -AsPlainText;" +
            $"Export-PfxCertificate" +
            $" -Cert $rootAuthorityPath" +
            $" -FilePath '{rootAuthorityPfxPath}'" +
            $" -Password $pwd";
        powerShell.AddScript(exportAuthorityPfxScript);

        // Create the self-signed certificate, signed using the above certificate
        var gatewayAuthorityName = "My-Service";
        var gatewayFriendlyName = "My Service";
        var gatewayAuthorityScript =
            $"$rootcert = ( Get-ChildItem -Path $rootAuthorityPath );" +
            $"$gatewayCert = New-SelfSignedCertificate" +
            $" -DnsName '{gatewayAuthorityName}'" +
            $" -NotAfter '{notAfter}'" +
            $" -certstorelocation cert:\\localmachine\\my" +
            $" -Signer $rootcert" +
            $" -FriendlyName '{gatewayFriendlyName}'" +
            $" -KeyUsage KeyEncipherment,DigitalSignature";
        powerShell.AddScript(gatewayAuthorityScript);

        // Export new certificate public key as a CRT file
        var myGatewayCrtPath = Path.Combine(saveDir, "MyGatewayAuthority.crt");
        var exportCrtScript =
            $"$gatewayCertPath = 'cert:\\localMachine\\my\\' + $gatewayCert.thumbprint;" +
            $"Export-Certificate" +
            $" -Cert $gatewayCertPath" +
            $" -FilePath {myGatewayCrtPath}";
        powerShell.AddScript(exportCrtScript);

        // Export the new certificate as a PFX file
        var myGatewayPfxPath = Path.Combine(saveDir, "MyGatewayAuthority.pfx");
        var exportPfxScript =
            $"Export-PfxCertificate" +
            $" -Cert $gatewayCertPath" +
            $" -FilePath {myGatewayPfxPath}" +
            $" -Password $pwd"; // Use the previous password
        powerShell.AddScript(exportPfxScript);

        powerShell.Invoke();
    }
}
Run Code Online (Sandbox Code Playgroud)

需要 PowerShell 4 或更高版本。


归档时间:

查看次数:

59089 次

最近记录:

6 年,2 月 前