How would I generate the Identity Server signing certificate

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 16:46:22

Get a dedicated cert - either via your PKI or self-generate one:

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

Import the key pair into the Windows certificate store, and load it from there at runtime.

To step up security, some people deploy the keys to a dedicated device (called an HSM) or to a dedicated machine (e.g. behind a firewall). The ITokenSigningService allows moving the actual token signing to that separate machine.

For the record, the code proposed in the image posted by 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];
    }
}

Here is how I load it from a thumbprint in my config: Click here to see image

Recently I decided to revamp my token signing issuing process. If you're running Windows 10, you can use the awesome powershell cmdlet called New-SelfSignedCertificate.

Here is my example usage:

    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"

Make sure you are running the command as an admin. You can obtain the certificate details by opening certlm.msc. It should be stored below Personal\Certificates.

Most of the flags should be obvious, apart from the -TextExtention one. It specifies that an Enhaced Key Usage field is set to the "Code Signing" value. You can play around with the algorithm used, key length, even add extentisons by refering to the following documentation page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!