6

我有一个 MVC 应用程序,它部署到在 Windows Server 2012 虚拟机上运行的 Windows Azure 托管服务。在该web.config文件中,我有 3 个部分使用PKCS12ProtectedConfigurationProvider: connectionStringsdataCacheClientssystem.net/mailSettings/smtp. 以下是相关部分的样子:

<configuration>
  ...
  <configProtectedData>
    <providers>
      <add name="CustomProvider" thumbprint="[this is secret]"
          type="Pkcs12ProtectedConfigurationProvider.Pkcs12ProtectedConfigurationProvider, PKCS12ProtectedConfigurationProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=34da007ac91f901d" />
    </providers>
  </configProtectedData>
  ...
  <connectionStrings configProtectionProvider="CustomProvider">
    <EncryptedData ... ommitted for brevity
  </connectionStrings>
  ...
  <system.net>
    <mailSettings>
      <smtp configProtectionProvider="CustomProvider">
        <EncryptedData ommitted for brevity
      </smtp>
    </mailSettings>
  </system.net>
  ...
  <dataCacheClients configProtectionProvider="CustomProvider">
    <EncryptedData ommitted for brevity
  </dataCacheClients>
  ...
</configuration>

以上所有工作都很好。部署到 Azure 时,连接字符串、SMTP 邮件和数据缓存都可以正常工作。提供PKCS12ProtectedConfiguration者使用我的自定义证书来解密这些部分,一切都很好。

但是我似乎不能使用相同的方法来加密web.config/appSettings。当我尝试将以下内容部署到 Azure 时...

<configuration>
  ...
  <appSettings configProtectionProvider="CustomProvider">
    <EncryptedData ommitted for brevity
  </appSettings>
  ...
</configuration>

...然后我得到以下异常:

Server Error in '/' Application.

Configuration Error

Description: An error occurred during the processing of a configuration file
required to service this request. Please review the specific error details
below and modify your configuration file appropriately.

Parser Error Message: An error occurred loading a configuration file: Could not
load file or assembly 'PKCS12ProtectedConfigurationProvider, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=34da007ac91f901d' or one of its dependencies.
The system cannot find the file specified.

Source Error: 

Line 41:     </EncryptedData>
Line 42:   </connectionStrings>
Line 43:   <appSettings configProtectionProvider="CustomProvider">
Line 44:     <EncryptedData ...>
Line 45:       <EncryptionMethod .../>

Source File: E:\sitesroot\0\web.config    Line: 43 

Assembly Load Trace: The following information can be helpful to determine why
the assembly 'PKCS12ProtectedConfigurationProvider, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=34da007ac91f901d' could not be loaded.

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure
logging.
To turn this feature off, remove the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog].

但是我知道PKCS12ProtectedConfigurationProvider.dll它在/bin文件夹中,因为:

  1. 我远程进入虚拟机并在两者中都看到了approot/binsiteroot/bin
  2. 当我使用未加密的版本进行部署时appSettings,其他 3 个部分已使用此 dll 成功解密。

就好像程序集加载器PKCS12ProtectedConfigurationProvider.dll在解析并加载该appSettings部分之前无法查找该文件。我尝试省略提供程序配置部分的版本、文化和 PublicKeyToken 部分,但随后错误变为:

Parser Error Message: An error occurred loading a configuration file: Could not 
load file or assembly 'PKCS12ProtectedConfigurationProvider' or one of its 
dependencies. The system cannot find the file specified.

部署到在 Windows Server 2012 虚拟机上运行的 Windows Azure 托管服务时,是否可以web.config/appSettings使用自定义进行加密?configProtectionProvider如果是这样,我在这里错过了什么?

更新:

发布此消息后,我打开了Fusion!EnableLog注册表项,现在我在异常中获得了以下附加信息:

Assembly Load Trace: The following information can be helpful to determine why
the assembly 'PKCS12ProtectedConfigurationProvider, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=34da007ac91f901d' could not be loaded.

=== Pre-bind state information ===
LOG: User = NT AUTHORITY\NETWORK SERVICE
LOG: DisplayName = PKCS12ProtectedConfigurationProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=34da007ac91f901d
 (Fully-specified)
LOG: Appbase = file:///d:/windows/system32/inetsrv/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: D:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from D:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: PKCS12ProtectedConfigurationProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=34da007ac91f901d
LOG: Attempting download of new URL file:///d:/windows/system32/inetsrv/PKCS12ProtectedConfigurationProvider.DLL.
LOG: Attempting download of new URL file:///d:/windows/system32/inetsrv/PKCS12ProtectedConfigurationProvider/PKCS12ProtectedConfigurationProvider.DLL.
LOG: Attempting download of new URL file:///d:/windows/system32/inetsrv/PKCS12ProtectedConfigurationProvider.EXE.
LOG: Attempting download of new URL file:///d:/windows/system32/inetsrv/PKCS12ProtectedConfigurationProvider/PKCS12ProtectedConfigurationProvider.EXE.

所以这里有另一个问题:为什么 IIS 会在inetsrv这个程序集的路径中查找,而不是在应用程序的/bin文件夹中查找它?我在日志中看到“未找到应用程序配置文件”。这是否意味着程序集绑定器必须找到一个web.config未加密的appSettings部分才能解析和加载它?

4

2 回答 2

5

您是否尝试在角色启动时在 GAC 中安装程序集?

有关在启动时将程序集添加到 gac 的信息http://blogs.infosupport.com/adding-assemblies-to-the-gac-in-windows-azure/

于 2013-03-01T12:37:30.457 回答
0

我能够通过安装 NuGet 包来修复此错误:https ://www.nuget.org/packages/Pkcs12ProtectedConfigurationProvider/

我猜安装过程的一部分是为您注册 .dll。

于 2016-11-17T15:17:33.227 回答