Matthew
Matthew

Reputation: 4607

Null Refernce Exception after encrypting web.config file using Data Protection API

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

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"

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.

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

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>

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 :)

Clarification

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

Upvotes: 1

Views: 810

Answers (1)

Martin Munch
Martin Munch

Reputation: 26

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.

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

With kind regards, Martin

Upvotes: 1

Related Questions