我遇到了以下问题。
我运行了以下代码
var binaryData = File.ReadAllBytes(pathToPfxFile);
var cert = new X509Certificate2(binaryData, password);
在两个进程中。其中一个进程在 LOCAL_SYSTEM
下运行,这段代码可以成功执行。另一个进程在 IIS 中以属于 "Users" 本地组的本地用户帐户身份运行,在那里我会收到以下异常:
in two processes. One of the processes runs under LOCAL_SYSTEM
and there this code succeeds. Another one runs inside IIS under a local user account belonging to "Users" local group and there I get the following exception:
System.Security.Cryptography.CryptographicException
Object was not found.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)
//my code here
所以我谷歌了一下,找到了这个答案,针对一个类似的问题。我试着为应用程序池启用LoadUserProfile
,现在它可以工作了。
问题是我不知道当我设置LoadUserProfile
时究竟发生了什么,以及可能会有什么后果。我的意思是,如果这是一个“好”事情,那么为什么它不是默认打开的?它到底是干什么用的?
当我在IIS池中设置LoadUserProfile
时,究竟会发生什么,可能会产生哪些负面影响?
new X509Certificate2(binaryData, password, X509KeyStorageFlags.MachineKeySet)
,这样就不需要用户配置文件了。 - vcsjonesnew X509Certificate2(keyFilePath, keyFilePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet)
,这将避免将私钥写入存储并且不需要在服务器上具备管理员权限。 - Nate