20

In the identity server samples we find code like this in Startup.cs

var certFile = env.ApplicationBasePath + "\\idsrv3test.pfx";

var signingCertificate = new X509Certificate2(certFile, "idsrv3test");

How would I go about replacing this for production scenarios?

4

4 回答 4

16

作为记录,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];
    }
}
于 2016-07-12T15:47:40.520 回答
12

获取专用证书 - 通过您的 PKI 或自行生成的证书:

http://brockallen.com/2015/06/01/makecert-and-creating-ssl-or-signing-certificates/

将密钥对导入 Windows 证书存储,并在运行时从那里加载。

为了提高安全性,有些人将密钥部署到专用设备(称为 HSM)或专用机器(例如防火墙后面)。这ITokenSigningService允许将实际的令牌签名移动到该单独的机器上。

于 2016-03-10T06:33:29.387 回答
8

最近我决定改进我的令牌签名发行流程。如果您运行的是 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"

确保您以管理员身份运行该命令。您可以通过打开certlm.msc获取证书详细信息。它应该存储在 Personal\Certificates 下面。

大多数标志应该是显而易见的,除了 -TextExtention 之一。它指定将 Enhaced Key Usage 字段设置为“代码签名”值。您可以通过参考以下文档页面来使用所使用的算法、密钥长度,甚至添加扩展。

于 2018-01-16T15:21:29.850 回答
6

这是我从配置中的指纹加载它的方法: 单击此处查看图像

于 2016-03-10T03:13:06.547 回答