【发布时间】:2013-06-13 12:03:01
【问题描述】:
我遇到了以下问题。
我运行以下代码
var binaryData = File.ReadAllBytes(pathToPfxFile);
var cert = new X509Certificate2(binaryData, password);
在两个过程中。其中一个进程在LOCAL_SYSTEM 下运行,并且此代码成功。另一个在属于“用户”本地组的本地用户帐户下在 IIS 中运行,我得到以下异常:
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
所以我用谷歌搜索了一下,发现this answer 有一个类似的问题。我尝试为应用程序池启用LoadUserProfile,它现在可以工作了。
问题是我不知道设置LoadUserProfile 时到底发生了什么以及可能产生的后果。我的意思是,如果它是“好”的东西,那么为什么默认情况下它不是“开启”的,为什么它毕竟存在?
当我在 IIS 池中设置 LoadUserProfile 时究竟会发生什么,它会产生什么负面后果?
【问题讨论】:
-
只是事后诸葛亮,如果加载用户配置文件对您来说有问题,您可以将您的证书加载更改为
new X509Certificate2(binaryData, password, X509KeyStorageFlags.MachineKeySet),这样就不需要用户配置文件了。 -
如果作为 ApplicationPoolIdentity 运行,您将需要使用
new X509Certificate2(keyFilePath, keyFilePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet),这样可以避免将私钥写入存储并且不需要服务器上的管理权限。
标签: c# .net iis-7 x509certificate application-pool