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支持的步骤如下:
更新C#应用代码以包含https前缀.例
String[] prefixes = { "http://*:8089/","https://*:8443/" };
Run Code Online (Sandbox Code Playgroud)
从代码方面来看就是这样.
对于证书方面的东西,使用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.
将证书绑定到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值
可能还有其他方法可以实现上述目标,但这对我有用.
jpa*_*ugh 31
以下是我在Windows上设置独立服务器时使用的步骤,HTTPListener用于为HttpListener openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365应用程序创建自签名证书.如果您想进一步研究,它包含大量链接.
在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)创建自签名证书:*
*有关替代方案的使用Certificates,请参阅Walter自己的答案.
打开本地计算机的证书管理器.当您运行时Computer Account,它会打开当前用户的证书管理器,这不是我们想要的.代替:
Local Computerpfx,然后单击Add >mmc,然后单击NextPersonal.点击Finish,然后Okay将证书(Personal Information Exchange)导入目标计算机上的Windows证书存储区
All Files之前打开的窗口中,向下钻取Certificates (Local Computer) > PersonalPlace all certificates in the following store,然后单击All Tasks -> Import...Personal或Trusted Root Certification Authorities以便查找它netsh.验证它是否显示httpcfg,然后单击Finishcmd.exe证书部分重复上面的导入过程.为您的应用程序创建端口关联.在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接口)启动你的网络服务器,你很高兴!
小智 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 或更高版本。需要管理员权限。
我们可以使用 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 次 |
| 最近记录: |