encrypt SQL connectionstring c#

前端 未结 5 1068
离开以前
离开以前 2020-11-30 09:21

I created an c# application (not asp webpage) which connects to a sql 2005 server. In my sourcecode the password and userid for this sql-server is coded plain text in Connec

5条回答
  •  借酒劲吻你
    2020-11-30 09:43

    There are two ways of doing it:

    1) You can use Configuration Secure Section to encrypt and decrypt connection strimng from your source code:

    try
        {
            // Open the configuration file and retrieve 
            // the connectionStrings section.
            Configuration config = ConfigurationManager.
                OpenExeConfiguration(exeConfigName);
    
            ConnectionStringsSection section =
                config.GetSection("connectionStrings")
                as ConnectionStringsSection;
    
            if (section.SectionInformation.IsProtected)
            {
                // Remove encryption.
                section.SectionInformation.UnprotectSection();
            }
            else
            {
                // Encrypt the section.
                section.SectionInformation.ProtectSection(
                    "DataProtectionConfigurationProvider");
            }
            // Save the current configuration.
            config.Save();
    
            Console.WriteLine("Protected={0}",
                section.SectionInformation.IsProtected);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    

    2) You can Enterprise Library Data Access Application Block to perform the encryption using RSAProtectedConfigurationProvider or DPAPIProtectedConfigurationProvider.

    For a full articvle go to --> http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx

提交回复
热议问题