I希望能在本地群集上使用SSL运行SF,但我有些误解。 Microsoft创建了一篇关于配置端点 HTTPS 的文章,但它只在你使用他们的证书生成器
我已经得到了预期的结果。显示有关网站安全证书警告的页面:具有警告的ValueController结果 第三步,我创建了Service Fabric应用程序(无状态ASP.NET Core模板)。通过编辑Endpoint部分来更改我的ServiceManifest.xml文件:
CertSetup.ps1
时才有效。如果您尝试安装自己的 pfx,它将无法工作。
首先,我通过 OpenSSL 创建了本地主机自签名证书:set OPENSSL_CONF=W:\OpenSSL-Win32\bin\openssl.cfg
openssl genrsa -out W:\CERTS\wepapissl.key -passout pass:1234567890 -aes256 2048
openssl req -x509 -new -key W:\CERTS\wepapissl.key -days 10000 -out W:\CERTS\wepapissl.crt -passin pass:1234567890 -subj /CN="localhost"
openssl pkcs12 -export -inkey W:\CERTS\wepapissl.key -in W:\CERTS\wepapissl.crt -out W:\CERTS\wepapissl.pfx -passout pass:0987654321 -passin pass:1234567890`
第二步,我创建了一个默认的ASP.NET Core Web应用程序(Core 2.0, API模板)。并添加了配置Kestrel使用HTTPS的代码:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(opt =>
{
opt.Listen(IPAddress.Any, port, listenOptions =>
{
listenOptions.UseHttps(GetCertificateFromStore());
});
})
.UseStartup<Startup>()
.Build();
private static X509Certificate2 GetCertificateFromStore()
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=localhost", false);
return currentCerts.Count == 0 ? null : currentCerts[0];
}
finally
{
store.Close();
}
}
我已经得到了预期的结果。显示有关网站安全证书警告的页面:具有警告的ValueController结果 第三步,我创建了Service Fabric应用程序(无状态ASP.NET Core模板)。通过编辑Endpoint部分来更改我的ServiceManifest.xml文件:
<Endpoint Protocol="https" Name="ServiceEndpoint" Type="Input" Port="8256" />
同时添加了配置Kestrel使用HTTPS的代码(class Web1 : StatelessService
):
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
return new WebHostBuilder()
.UseKestrel(opt =>
{
int port = serviceContext.CodePackageActivationContext.GetEndpoint("ServiceEndpoint").Port;
opt.Listen(IPAddress.IPv6Any, port, listenOptions =>
{
listenOptions.UseHttps(this.GetCertificateFromStore());
});
})
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}
private X509Certificate2 GetCertificateFromStore()
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=localhost", false);
return currentCerts.Count == 0 ? null : currentCerts[0];
}
finally
{
store.Close();
}
}
结果: 成功在本地SF集群上构建和部署代码。但我的资源无法访问
附言:我再次重申,如果你使用Mircosoft提供的PowerShell - CertSetup.ps1
安装新证书,它对SF应用程序有效。我试着挖掘PS脚本,但我不明白我错过了什么。
附言2:我是新手创建证书,但它看起来很奇怪。
- 我通过
CertSetup.ps1
安装了pfx证书。所有工作正常(资源可访问)。 - 然后我导出了带有私钥和所有扩展属性的pfx证书。
- 从LocalMachine (MY 和 Root),CurrentUser (MY)存储中删除
- 将导出的pfx安装到LocalMachine (My 和 Root),CurrentUser (My)存储中
- 重新构建并重新部署代码
- 资源无法访问
这是魔法吗?还是我漏掉了什么?