如何加密解密连接字符串并将其分配给Entity Framework

时间:2015-11-05 17:09:50

标签: asp.net vb.net entity-framework encryption entity-framework-4

我有一个使用实体框架的asp.net表单网站。 我需要加密Web配置中的连接字符串,以便人们看不到它。 我还需要解密实体框架的连接字符串才能使用它。

我正在关注此article

确实运行了生成的命令aspnet_regiis -pef "connectionStrings" "C:\Projects\students\exams.UI"替换了我原来的连接字符串

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

使用以下连接字符串

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

我使用以下代码来获取解密密钥

 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

如何解密获取连接字符串设置并将其分配给实体框架。

0 个答案:

没有答案