Kla*_*hen 11 windows asp.net https ssl-certificate asp.net-core-2.0
我写了一个小的ASP.NET Core 2应用程序.它作为服务运行,因此没有IIS.它在运行Windows 7 SP1的PC上运行.
var host = WebHost.CreateDefaultBuilder(args)
.UseContentRoot(pathToContentRoot)
.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.None;
options.Authentication.AllowAnonymous = true;
options.MaxConnections = null;
options.MaxRequestBodySize = 30000000;
options.UrlPrefixes.Add("http://*:5050");
})
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
if (isService)
{
host.RunAsService();
}
else
{
host.Run();
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我想听5050端口.没有SSL就可以正常工作.
我的问题是,如何为我的应用程序启用https?再次:没有IIS,没有域名(没有互联网连接).通信就在内部网络内部,因此我想使用自签名证书.
我阅读了文档(HTTP.sys文档 ; Netsh命令 ; New-SelfSignedCertificate),但总有一些与我的情况不同(他们使用Krestel,或者是使用IIS).另外,我不知道如何为我的应用程序获取App-ID(netsh所需).我试过这个:StackOverflow获取GUID但它不起作用.
var assembly = typeof(Program).Assembly;
// following line produces: System.IndexOutOfRangeException
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
var id = attribute.Value;
Console.WriteLine(id);
Run Code Online (Sandbox Code Playgroud)
所以我对所有可能性和不同配置感到有点困惑.文档不考虑我的具体情况.
我创建了一个证书,我想我需要将它存储在"我的"商店中.(那是什么?cert:\ LocalMachine\My)然后我需要将我的Applicaion ID和端口分配给它.
但我不知道该怎么做.有人可以帮忙吗?
所以我通过以下方式解决问题:
首先,如果您想知道自己的 GUID,您将通过以下代码获得它:
var id = typeof(RuntimeEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<GuidAttribute>().Value;
Run Code Online (Sandbox Code Playgroud)
现在创建一个自签名证书(如果您已经拥有或购买了请跳过此步骤)
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
openssl pkcs12 -inkey key.pem -in certificate.pem -export -out certificate.p12
对于 Windows 8 及更高版本:
使用PowerShell将证书添加到 Windows 证书存储
PS C:> $certpwd = ConvertTo-SecureString -String "passwort" -Force –AsPlainText
PS C:> Import-PfxCertificate –FilePath D:\data\cert\certificate.p12 cert:\localMachine\my -Password $certpwd
获取证书的指纹(哈希)
PS C:\WINDOWS\system32> dir Cert:\LocalMachine\my
安装证书(用你的值替换哈希、IP 和端口)
PS C:\WINDOWS\system32> $guid = [guid]::NewGuid()
PS C:\WINDOWS\system32> $certHash = "A1D...B672E"
PS C:\WINDOWS\system32> $ip = "0.0.0.0"
PS C:\WINDOWS\system32> $port = "5050"
PS C:\WINDOWS\system32> "http add sslcert ipport=$($ip):$port certhash=$certHash appid={$guid}" | 网申
你完成了。
对于 Windows 7
将证书添加到 Windows Cert Store (注意:此操作使用 .pem 文件,因为 certutil 似乎不支持 .p12 文件)
.\certutil.exe -addstore -enterprise -f "Root" C:\lwe\cert\certificate.pem
如果他的线路抛出以下错误:
SSL 证书添加失败,错误 1312 指定的登录会话不存在。它可能已经被终止。
您必须手动执行这些步骤(手动执行时请插入 .p12 文件,而不是 .pem):
运行mmc.exe
转到文件-> 添加/删除管理单元
选择证书管理单元。
选择计算机帐户
导航到:证书(本地计算机)\个人\证书
右键单击“证书”文件夹并选择“所有任务”->“导入”。
按照向导说明选择证书。确保在向导期间选中导出复选框。
要获取您的证书的哈希值,请运行 Internet Explorer,按 Alt + X 并转到 Internet 选项 -> 内容 -> 证书。搜索您的证书并读取哈希值。
现在您可以运行与 Windows 8+ 相同的命令:
安装证书(用你的值替换哈希、IP 和端口)
PS C:\WINDOWS\system32> $guid = [guid]::NewGuid()
PS C:\WINDOWS\system32> $certHash = "A1D...B672E"
PS C:\WINDOWS\system32> $ip = "0.0.0.0"
PS C:\WINDOWS\system32> $port = "5050"
PS C:\WINDOWS\system32> "http add sslcert ipport=$($ip):$port certhash=$certHash appid={$guid}" | 网申
毕竟,您必须将 UrlPrefixes 设置为 https。所以在你的 Program.cs 文件中你需要有:
var host = WebHost.CreateDefaultBuilder(args)
.UseContentRoot(pathToContentRoot)
.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.None;
options.Authentication.AllowAnonymous = true;
options.MaxConnections = null;
options.MaxRequestBodySize = 30000000;
options.UrlPrefixes.Add("https://*:5050");
})
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
Run Code Online (Sandbox Code Playgroud)