在身份验证服务器示例中,我们会在Startup.cs
中找到类似下面的代码
var certFile = env.ApplicationBasePath + "\\idsrv3test.pfx";
var signingCertificate = new X509Certificate2(certFile, "idsrv3test");
我该如何在生产环境中进行替换?
在身份验证服务器示例中,我们会在Startup.cs
中找到类似下面的代码
var certFile = env.ApplicationBasePath + "\\idsrv3test.pfx";
var signingCertificate = new X509Certificate2(certFile, "idsrv3test");
我该如何在生产环境中进行替换?
记录一下,RuSs发布的图片中提出的代码:
options.SigningCertificate = LoadCertificate();
public X509Certificate2 LoadCertificate()
{
string thumbPrint = "104A19DB7AEA7B438F553461D8155C65BBD6E2C0";
// Starting with the .NET Framework 4.6, X509Store implements IDisposable.
// On older .NET, store.Close should be called.
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, validOnly: false);
if (certCollection.Count == 0)
throw new Exception("No certificate found containing the specified thumbprint.");
return certCollection[0];
}
}
获得一个专用的证书 - 可以通过您的PKI或自行生成:
http://brockallen.com/2015/06/01/makecert-and-creating-ssl-or-signing-certificates/
将密钥对导入Windows证书存储,并在运行时从那里加载它。
为了提高安全性,一些人会将密钥部署到专用设备(称为HSM)或专用机器(例如,在防火墙后面)。ITokenSigningService
允许将实际令牌签名移动到该独立机器。
最近我决定改进我的令牌签名发放流程。如果你正在运行Windows 10,你可以使用名为New-SelfSignedCertificate的强大PowerShell cmdlet。
以下是我的示例用法:
New-SelfSignedCertificate -Type Custom
-Subject "CN=TokenSigningForIdServer"
-TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3")
-KeyUsage DigitalSignature
-KeyAlgorithm RSA
-KeyLength 2048
-CertStoreLocation "Cert:\LocalMachine\My"
X509Store
实现了IDisposable
,因此不需要调用store.Close()
。更多详情请参见https://dev59.com/YnbZa4cB1Zd3GeqPCy0o。 - angularrocks.com