本文介绍了使用Data Protection API加密web.config文件后的空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加密了我的网络应用程序的web.config文件的 appSettings connectionStrings 部分。

I have encrypted the appSettings and connectionStrings section of the web.config file of my web application.

这些是我在Visual Studio 2010命令提示符下输入的两个命令:

These are the two commands that I entered at the Visual Studio 2010 command prompt:

aspnet_regiis.exe -pef "connectionStrings" C:\Provider -prov "DataProtectionConfigurationProvider"

aspnet_regiis.exe -pef "appSettings" C:\Provider -prov "DataProtectionConfigurationProvider"

现在,这两个命令产生了一个位于目录中的新的web.config文件以及解决方案文件。我打开这个web.config文件,它只包含原始web.config文件的加密appSettings和connectionStrings部分。

Now, these two commands produced a new web.config file situated in the directory along with the solution file. I opened this web.config file, which only contained the encrypted appSettings and connectionStrings section of the original web.config file.

然后我打开了我的Web应用程序,删除了原来的 appSettings connectionStrings 部分,并粘贴加密的。

I then opened my web application, deleted the original appSettings and connectionStrings sections and pasted the encrypted ones.

这是我的web.config文件现在如何:

This is how my web.config file looks now:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>
        <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA6h2T0PWsHUC2CPpYvY8QUwQAAAACAAAAAAAQZgAAAAEAACAAAAAdlSIaGrQ1CFjswJi2RxekJ4ZxmRArilsOiqrmUXt6JgAAAAAOgAAAAAIAACAAAACaV/bVjlK60wX9LOFzRsrkbcDjSOT+3Qj0JyUZZszNNSAAAACaQC3oKCPX1gaxZK3ghS6lAMcVwpNpbMpyNpeoiwxap0AAAAD87rr8QUaIQJv2Sc+i+RGWq1+vExAPNjjG1VtWvK4ILsOX88iBRRx0tpAFdNAw0AvGoxUTA7UQGKm7hTHBaAMz</CipherValue>
      </CipherData>
    </EncryptedData>
  </appSettings>
  <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>
        <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA6h2T0PWsHUC2CPpYvY8QUwQAAAACAAAAAAAQZgAAAAEAACAAAAB4Y7QqEGRvo9T04hE8hvd3wMvRXqIMa/UJBkOQnMnsbgAAAAAOgAAAAAIAACAAAADnzwxmuoWUQLYJ0/YPUkgvR/xyXDZNaQI4ZrMmACqvaTAAAAC6C0nEhW+g8WHcNJLN5DRi8uNimkG3GyMEajrB33ST7DN49W925xIeMiN3kvyLAcJAAAAAPcgh+jh6RzsfQElj7/e1RNAQEFQykiqYfLbUEMd+qHcfkLCNwe3tczJQDckGH1cT7Y9At16pPfek1bKZeM7YpQ==</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>

  <system.web>
    <compilation debug="true" explicit="true" targetFramework="4.0"/>
    <httpCookies httpOnlyCookies="true" requireSSL="true"/>

    <customErrors mode="On" defaultRedirect="DefaultErrorPage.htm">
      <error statusCode="404" redirect="ErrorPage.htm"/>
    </customErrors>

    <trace enabled="false"/>
  </system.web>
</configuration>

现在我遇到的问题是当我尝试使用一个访问网络数据的页面.config文件(如连接字符串),我收到一个null引用异常。

The problem that I have now is that when I try to use a page which accesses data in the web.config file (such as the connection string), I am getting a null reference exception.

例如,这一行生成一个空引用异常:

For instance, this line generates a null reference exception:

string connection = ConfigurationManager.ConnectionStrings["DB_Connection"].ConnectionString;

我该如何解决?谢谢:)

How can I solve this please? Thank you :)

澄清

该行在使用数据保护API。

The line worked perfectly before encrypting using the Data Protection API. The null reference exception started cropping up after encryption.

推荐答案

不知道你是否解决了这个问题,但是对于我来说,解决方案是授予对C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys中创建的机器密钥文件的读取访问权限到帐户NT AUTHORITY\NETWORK SERVICE。

Don't know if you ever solved this but for me the solution was to grant read access to the machine key file created in "C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys" to the account NT AUTHORITY\NETWORK SERVICE.

NullReferenceException是由应用程序无法读取包含加密/解密密钥的文件引起的。

The NullReferenceException was caused by the application not being able to read the file containing the encryption/decryption key.

请客气,
Martin

With kind regards,Martin

这篇关于使用Data Protection API加密web.config文件后的空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 14:11