1

I have a asp.net forms website which uses entity Framework. I need to encrypt the connection string in the web config so that it is not visible to people. I also need to decrypt the connection string for entity framework to use it.

I am following this article.

The did run the command aspnet_regiis -pef "connectionStrings" "C:\Projects\students\exams.UI" which generated replaced my original connectionstring

<connectionStrings>
<add name="StudentEntities" connectionString="metadata=res://*/StudentModel.csdl|res://*/StudentModel.ssdl|res://*/StudentModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Server;initial catalog=Student;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

with the following connection string

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <KeyName>Rsa Key</KeyName>
          </KeyInfo>
          <CipherData>
            <CipherValue>aXfgsT4DI=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>eCAX0N2hgsldWCTr</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>

I use the following code to get the decrypt key back

 Dim setting = ConfigurationManager.ConnectionStrings


            Dim config As System.Configuration.Configuration = _
              WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
            Dim key As MachineKeySection = CType(config.GetSection("system.web/machineKey"), MachineKeySection)

            DecryptionKey = key.DecryptionKey
            ValidationKey = key.ValidationKey

how do I decrypt get the connection string settings and assign it to Entity Framework.

2
  • Possible duplicate of Entity Framework Encrypt Connection String Commented Nov 5, 2015 at 19:10
  • The framework will take care of the decrypting at runtime when you encrypt the web.config file. Commented Nov 5, 2015 at 19:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.