如何生成Identity Server签名证书

21

在身份验证服务器示例中,我们会在Startup.cs中找到类似下面的代码

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

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

我该如何在生产环境中进行替换?

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];
    }
}

2
在.NET 4.6中,X509Store实现了IDisposable,因此不需要调用store.Close()。更多详情请参见https://dev59.com/YnbZa4cB1Zd3GeqPCy0o。 - angularrocks.com

12

获得一个专用的证书 - 可以通过您的PKI或自行生成:

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

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

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


9

最近我决定改进我的令牌签名发放流程。如果你正在运行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 下面。
大多数标志应该很明显,除了 -TextExtension。它指定一个增强密钥使用字段设置为 "Code Signing" 值。您可以参考以下文档页面来尝试使用不同的算法、密钥长度甚至添加扩展:documentation

6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接