从C#代码加密/解密连接字符串 [英] Encrypting/Decrypting connection string from C# code

查看:82
本文介绍了从C#代码加密/解密连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以从我的C#代码中实现对连接字符串的加密和解密.

Is there any way I can achieve this encrytption and decryption of connection string from my C# code.

像我们一样

aspnet_regiis -pe"connectionStrings"

aspnet_regiis -pe "connectionStrings"

aspnet_regiis -pd"connectionStrings"

aspnet_regiis -pd "connectionStrings"

谢谢

推荐答案

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);
}

这篇关于从C#代码加密/解密连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆