2016-06-23 145 views
1

我想從WPF應用程序中的pfx文件加載證書,它給我一個訪問被拒絕的錯誤。CryptographicException:拒絕訪問 - 如何授予用戶存儲上的訪問權限?

using (FileStream stream = System.IO.File.OpenRead(certificatePath)) 
{ 
    using (BinaryReader reader = new BinaryReader(stream)) 
    { 
     buffer = reader.ReadBytes((int)stream.Length); 
    } 
} 

X509Certificate2 certificate = new X509Certificate2(buffer, password); 

System.Security.Cryptography.CryptographicException:拒絕訪問。
在 System.Security.Cryptography.CryptographicException.ThrowCryptographicException(的Int32 小時)在 System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(字節[] RAWDATA,IntPtr的密碼,UInt32的dwFlags中,布爾persistKeySet, SafeCertContextHandle & pCertCtx)在 System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(字節[] RAWDATA,對象密碼,X509KeyStorageFlags keyStorageFlags)在 System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(字節[] RAWDATA,字符串密碼)在 HelloWorld.HelloClient.Models.Infrastructure。 ReadCertificateFromPfxFile(字符串 certificatePath,字符串密碼)

在片段中的最後一行是引起異常,如果我運行它作爲管理員,它工作正常。這個問題似乎是X509Certificate2的默認構造函數嘗試將私鑰放入用戶存儲中。我沒有使用網絡應用程序。 this發佈無法解決我的問題。我認爲當前用戶可能無法訪問他自己的私人密鑰存儲區。但我怎麼能給這個訪問?

+0

可能重複的[X509Certificate構造函數異常](http://stackoverflow.com/questions/9951729/x509certificate-constructor-exception) – MethodMan

+0

@MethodMan它談論的Web應用程序和IIS中的修復。我有WPF應用程序如何授予用戶存儲權限? –

回答

1

如果有人在尋找類似問題的解決方案,請發佈修復程序。我運行sysinternal進程監視器,並意識到構造函數調用是在機器密鑰文件夾中創建一個密鑰,並授予用戶在機器密鑰上寫入的權限。

+0

這是我的問題。感謝您的解決方案! –

相關問題