![](/img/trans.png)
[英]X509Certificate2.PrivateKey as RSACryptoServiceProvider returns null in .Net Standards 2.1
[英].NET Core X509Certificate2.PrivateKey throws nte_bad_keyset error
嘗試使用以下代碼從X509Store
獲取X509Certificate2
對象時:
private X509Certificate2 GetKey()
{
try
{
X509Store store = new X509Store("WebHosting", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var collection = store.Certificates.Find(X509FindType.FindBySubjectName, "xxxxxxx", true);
if (collection.Count == 0)
{
throw new Exception("No keys matched");
}
if (collection.Count > 1)
{
StringBuilder sb = new StringBuilder();
sb.Append("More than 1 key matched:\r\n");
foreach (var cert in collection)
{
sb.Append($"{cert.SubjectName} - {cert.Thumbprint}\r\n");
}
throw new Exception(sb.ToString());
}
return collection[0];
}
catch (Exception ex)
{
// something really bad happened, log it
Logger.LogException(ex);
throw;
}
}
我成功地獲得了鑰匙; 但是,當嘗試使用key.PrivateKey
獲取對象內的key.PrivateKey
,我收到以下錯誤: OpenCSP failed with error code 2148073494.
查找Windows錯誤2148073494,我得到nte_bad_keyset
。 看起來在其他情況下出現了錯誤,這些錯誤會在這里引發同樣的錯誤,但是他們將這個錯誤關閉了。 當我在控制台應用程序中運行這段代碼時,它工作正常,它也可以在我在IISExpress下運行的測試環境中正常工作。 在IIS下的生產環境中運行時,每次都會出現此錯誤。 我嘗試在管理員用戶的上下文中運行,以確保它不是一個奇怪的權限錯誤,同樣的事情。 根據我對這個Windows錯誤的理解,是Windows給了我密鑰所在的位置,然后告訴我該地址沒有任何內容。 我正在使用"System.Security.Cryptography.Algorithms": "4.3.0"
。
編輯:我應該注意,作為我測試的一部分,我實際上從生產環境中獲取了我正在尋找的確切證書到我的測試環境中並且它加載得很好。 我還在生產環境中運行控制台應用程序,拉動相同的密鑰,它工作正常。
最終,答案是“在將私鑰導入證書存儲區后刪除了私鑰”(或者,也許可能會混淆Windows以記住密鑰存在的位置,盡管它實際上並不存在於那里)。
例如,如果你知道它,它可以工作一段時間然后停止:
> certutil -store my
...
================ Certificate 6 ================
Serial Number: 3451b93c10f9279348a949f729d1ff10
Issuer: CN=localhost
NotBefore: 1/26/2015 2:19 PM
NotAfter: 1/25/2020 4:00 PM
Subject: CN=localhost
Signature matches Public Key
Root Certificate: Subject matches Issuer
Template:
Cert Hash(sha1): 15 e3 4c d3 2d a7 54 99 a9 17 8f 17 26 25 63 25 8f 3a 94 28
Key Container = IIS Express Development Certificate Container
Unique container name: fad662b360941f26a1193357aab3c12d_1fcb2e07-cec4-4ba1-9c78-58a431e1aefd
Provider = Microsoft RSA SChannel Cryptographic Provider
Encryption test passed
CertUtil: -store command completed successfully.
看到它在“Microsoft RSA SChannel Cryptographic Provider”中,請轉到https://msdn.microsoft.com/en-us/library/windows/desktop/bb204778(v=vs.85).aspx並查看密鑰文件將位於%ALLUSERSPROFILE%\\Application Data\\Microsoft\\Crypto\\RSA\\MachineKeys
。 Unique container name
恰好是它將擁有的文件的名稱。
因此,打開該目錄,右鍵單擊該文件。
稍后,在開始獲取密鑰集錯誤后,在安全日志中搜索文件刪除審核(來自安全審核的事件4663):
An attempt was made to access an object.
Subject:
Security ID: SOMEDOMAIN\theaccount
Account Name: theaccount
Account Domain: SOMEDOMAIN
Logon ID: 0xabcdef
Object:
Object Server: Security
Object Type: File
Object Name: C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\fad662b360941f26a1193357aab3c12d_1fcb2e07-cec4-4ba1-9c78-58a431e1aefd
Handle ID: 0xef8
Process Information:
Process ID: 0xf54
Process Name: C:\Windows\explorer.exe
Access Request Information:
Accesses: DELETE
Access Mask: 0x10000
這將告訴你什么進程/用戶執行了刪除操作...也許這足以識別出錯的地方。
您可以通過編程方式更准確地進行私鑰文件識別和審計注冊; 但這是我所知道解釋它的最快方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.