In my .net web application I encrypted the of the web.config file.
But now I need to read this encrypted connection String from an external winforms application and display the result in a text box . The text box text would look something like this :
<connectionStrings configProtectionProvider="CustomEncryptProvider">
<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>Smbfcf3dHbuYZ34hLWtATU8fBqNL/CvPk24tLj9gLOizLzV88den52yhtYQ5AwXe2vVZP/GKRsWB+rcX/6ufBkz75HyVOnJHTCgLQ+JcsRX/9Td5ZzWJrEq1JBdpFzBsS9aLGLMREIILPedmFxO5+0GLIaBPzZ9/BhNcN8GXa+k=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>raoQqDzlXmMCy+3VliV6oyMoQzgIapSmBKw666WbUjLgurCh4aS+pwSMW3wULOpi+jh8BdDE/aPwvhDw9kTuComyHBsEB4xMtRFaBY1NSyrwx7dnP44x4NS+LowJ1EQiN2fAZqWDDVAljRIlq3DtZhC9YkYl4H1rEjQVvljD0pus1O8ftiqKy/yma1/rqzI+F/87GrFR1ZM8cS/ujXagtfzqME4iVdTgl/eyEPkrd5f6SGwlieeC0zJ2ErV9zIr+Af2Sc6mk2hz7/+t2x3kAzDzHU2PFfBqiLSP6o/0XAdRl43Q/Jwr72552mus7n5urlzvyND0KXKzk4Gg4bVYuo8sSQvphbFuLgHIxq+6ShDdCc9wfMzsBmGU4ayYbn/a4rI8lB5y6GzK0kQvnH0qtWQ==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
How to achieve the same ? Please note I only have the physical path to the web.config file .
You can load a config file using System.Configuration.ConfigurationManager
like this:
var config = ConfigurationManager.OpenExeConfiguration(/* path to config file */);
The connection strings will automatically be decrypted, and will be available in the ConnectionStrings
property of the Configuration
object. In your case, the connection string is called "LocalSqlServer":
Console.WriteLine(config.ConnectionStrings.ConnectionStrings["LocalSqlServer"]);
> data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
EDIT
If, as you've indicated, you actually want the whole content of the connectionStrings
xml element, you can do this with an XmlReader
:
using (var reader = XmlReader.Create(/* path to config file */))
{
if (reader.ReadToDescendant("connectionStrings"))
Console.WriteLine(reader.ReadOuterXml());
}
> <connectionStrings configProtectionProvider="CustomEncryptProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" etc...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.