118

我遇到了以下问题。

我运行以下代码

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

所以我用谷歌搜索了一下,找到了一个类似问题的答案。我尝试启用LoadUserProfile应用程序池,它现在可以工作了。

问题是我不知道设置时究竟会发生LoadUserProfile什么以及可能产生的后果。我的意思是,如果它是一个“好”的东西,那么为什么它默认不“打开”,为什么它毕竟在那里?

当我在 IIS 池中设置时究竟会发生LoadUserProfile什么,它会产生什么负面后果?

4

1 回答 1

134

我的意思是,如果它是一个“好”的东西,那么为什么它默认不“打开”,为什么它毕竟在那里?

IIS 6 从不加载用户配置文件。我假设默认情况下这是关闭的以保持行为一致,并且管理员必须选择加入它。

我尝试为应用程序池启用 LoadUserProfile,它现在可以工作了。

这很可能是因为 Windows 加密服务提供程序试图在用户存储中存储或加载您的证书的密钥,并且由于配置文件不可用,因此加密上下文不可用。请注意,该Load User Profile设置仅适用于用户帐户。NETWORK SERVICE 和 ApplicationPoolIdentity 等服务帐户有特殊处理。

当我在 IIS 池中设置 LoadUserProfile 时究竟会发生什么

好吧,用户配置文件已加载。这包括它们的加密存储、环境变量(例如 %TEMP%)和其他变量。

它最终归结为LoadUserProfile在 AppPool 启动时由 IIS 调用。

它会产生什么负面后果?

它可能会破坏与在 IIS 6 上运行的应用程序的向后兼容性,该应用程序未加载用户配置文件。环境变量已加载。例如,当 Load User Profile 为 true 时,%TEMP% 环境变量为C:\Users\AccountName\AppData\Local\Temp(例如)。为假时,为C:\WINDOWS\Temp.

于 2013-06-17T14:19:42.960 回答