2016-03-08 68 views
16

在身份服務器樣本,我們發現在Startup.cs我怎麼會產生Identity Server的簽名證書

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

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

我怎麼會去更換這對於生產場景這樣的代碼?

回答

12

根據記錄,該代碼張貼拉斯圖像中提出:

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

in .NET 4.6'X509Store'實現'IDisposable',所以不需要'store.Close()'。更多細節在這裏http://stackoverflow.com/questions/18475558/what-is-the-implication-of-not-using-x509store-close-method – Kuncevic

+0

@Kuncevic:謝謝,我更新了示例。 –

0

最近我決定重整我的令牌簽名發放過程。如果您運行的是Windows 10,則可以使用名爲New-SelfSignedCertificate的awesome 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字段設置爲「代碼簽名」值。您可以使用所使用的算法,密鑰長度,甚至通過參考以下documentation頁面添加擴展。