通常都是把與數據庫的連接字符串放到web.config文件裡,因為有API可以直接訪問並取出數據,
但也存在一些安全問題,數據庫的連接字符串是以明文的方式在.config文件裡的。
.NET 裡有ASP.NET IIS 注冊工具 (Aspnet_regiis.exe) 這樣的一個工具,可以對站點的.config文件的節進行加密
方法:
加密:aspnet_regiis -pef "加密的web.config裡面的配置節名稱" "web.config文件所處的目錄"
解密:aspnet_regiis -pdf "加密的web.config裡面的配置節名稱" "web.config文件所處的目錄"
加密之前:
<connectionStrings>
<add name="pubsConnectionString" connectionString="Data Source=MHL\SQL2000;Initial Catalog=pubs;User ID=sa;Password=sql2000"
providerName="System.Data.SqlClient" />
</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>e15rhABrAtua53kjZ2a3U+ijC/Hr5ZhGUWGL5swkfxJaHS6hxGacw7CxyNlhnJwRc44ZE2edsKRfSJeXPS76fe4znlE5BqaxMMMWK+yFMiuWM+Cor1DFffheRNjAB7pMYdRQoMRCBq6H18gxIGlRY1dXtUERzNLJ5ug+S/qdj8E=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>Y1SjIK04jc0cS8Fg7EJyLoiXAUWc+I708SOsebWHeihg4kRFEF4wGvQLyrZQqFXNUIm56tIktQgapd7px92nfwAzYFv8R2gYV/7JwjfbjpM1v27p5WgNycJbo8H4uNj4LwgPfoqy2Dl41geMFHXCOak8IWxrA8MkbpIsKHFTJNkMo9yph+vVUMdViwaBSOKONSrXJ2k16kjHoSjJe1VP6WfMFZCeL7+nMuLf4sNZc34d0fl3S50H+kNwZre9MkUCSLr5pImCv5fhgHP+Ee9QwS8ahvOvRwUY7kWMr+M+jKoS+FDbhuRIkcbWaPP75XzdmyBN/vGwcMfX63faA9vdNOsl1a75Kd0L</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>