我对在 localhost 上使用 SSL 在本地集群上运行 SF 有一些误解。Microsoft 创建了有关在端点上配置 HTTPS 的精彩文章,但只有使用他们的证书生成器 CertSetup.ps1
才能正常工作。如果您尝试安装自己的 pfx,它将无法正常工作。
首先,我通过 OpenSSL 创建了 localhost 自签名证书:
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 模板)。ServiceManifest.xml
通过编辑Endpoint
部分更改我的:
<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 集群上成功构建和部署代码。但我的资源无法访问
PS 我再重复一遍,如果您使用 Mircosoft 提供的 PowerShell 安装新证书CertSetup.ps1
,它适用于 SF 应用程序。我试图挖掘 PS 脚本,但我无法理解我错过了什么。
PPS 我是创建证书的新手,但看起来很奇怪。
- 我已经安装了 pfx
CertSetup.ps1
。一切正常(资源可达)。 - 然后我用私钥和所有扩展属性将证书导出到 pfx
- 从 LocalMachine(MY 和 Root)、CurrentUser (MY) 存储中删除
- 将导出的 pfx 安装到 LocalMachine(My 和 Root)、CurrentUser(My)存储
- 重建和重新部署代码
- 资源无法到达
是魔法吗?还是我错过了什么?