简体   繁体   English

从C#代码加密/解密连接字符串

[英]Encrypting/Decrypting connection string from C# code

Is there any way I can achieve this encrytption and decryption of connection string from my C# code. 有什么办法可以从我的C#代码中实现对连接字符串的加密和解密。

like we do 像我们一样

aspnet_regiis -pe "connectionStrings" aspnet_regiis -pe“connectionStrings”

aspnet_regiis -pd "connectionStrings" aspnet_regiis -pd“connectionStrings”

Thanks 谢谢

static public void ProtectSection()
{
    // Get the current configuration file.
    System.Configuration.Configuration config =
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    // Get the section.
    UrlsSection section = (UrlsSection)config.GetSection("connectionStrings");

    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system
    // uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
    section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM