- Tạo một ứng dụng winform trong visual studio tên EncryptConfigFile
- Trong app.config ta có chuỗi connectionString sau, giờ ta sẽ mã hóa chuỗi kết nối "SecurePassDataBase" này
connectionstring="Data Source=D-6058;Initial Catalog=DEMO_Test;User ID=sysdba;Password=xxxxxx" name="SecurePassDataBase"
- Tạo form Encrypt gồm một openFileDialog và một button Encrypt : khi click browse openFileDialog sẽ chọn EncryptConfigFile.exe trong thư mục bin/Debug của ứng dụng sau đó click nút Encrypt để mã hóa chuỗi kết nối
- Viết function EncryptConnectionString như sau:
encrypt : true mã hóa, false là giải mã
fileName là file EncryptConfigFile.exe
public static void EncryptConnectionString(bool encrypt,string fileName)
{
Configuration configuration = null;
try
{
// Open the configuration file and retrieve the connectionStrings section. configuration = ConfigurationManager.OpenExeConfiguration(fileName);
ConnectionStringsSection configSection =
configuration.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) &&
(!(configSection.SectionInformation.IsLocked)))
{
if (encrypt && !configSection.SectionInformation.IsProtected)
{
//this line will encrypt the file configSection.SectionInformation.ProtectSection
("DataProtectionConfigurationProvider");
}
if (!encrypt &&
configSection.SectionInformation.IsProtected)//encrypt is true so encrypt {
//this line will decrypt the file. configSection.SectionInformation.UnprotectSection();
}
//re-save the configuration file section configSection.SectionInformation.ForceSave = true;
// Save the current configuration
configuration.Save();
Process.Start("notepad.exe", configuration.FilePath);
//configFile.FilePath }
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
- Chạy ứng dụng : kết quả sẽ trả về chuỗi kết nối đã mã hóa trong file notepad :
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/ClsBAAAATeylFe/xsUiVdcZvovEYDwQAAAACAAAAAAADZgAAwAAAABAAAABZsoaKP62hL85wpSO3znAAAAAASAAACgAAAAEAAAAHZ5NcKcDcWuEVDKyU4mz7J4AQAAAILD3fmIimyY2rkEkAdAtRn0dh9tI7Y5ILciikoSd/y2myUS88vJ59pIf82vOLk/0UwKL8TnHEaFTeX7SJ5par6pW7Pyhu4kKTEMyMUQsZX/h8RjNOntQ/kZIdqF2YWxFUP0RF3GWirvMNWS3do7IE0WaJ1W3wLHhalglmKURWIGHsvJlyblEGI8crPnli0W/yMNfR0P/ndaTY87kR4 0gvKDWzZ/dMh8E7ZtodFzTQ4pjpl5YyRHH/Tc3oFUtimCnzXvCVT4ykK6NEQfPiPc5KJW6ajTEEGOrAXTnr9HF2wCRekE3WUVPYkeHRTjtuf2hUyvYx4eoGeOIAzFFXxY1GzZqhl8YaHlukZagiTVbfXA6WhK0dsAiOPzwbCT92/blgsdkoKSMy8vRqFxAhX8HoW6KbJhsBPOvv36iBr1RecCpzUxWrVssSwi/JclVfVs0nYb/pFidcJwhuwBsS6IzvV1tgrk8F9CUor6DYHd/ABQAAABZjFi30hPRmKjpvxFzjeNHDhhg==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
- Copy chuỗi mã hóa này vào các ứng dụng ta cần chạy chuỗi kết nối đã mã hóa này
- Cách gọi chuỗi kết nối vẫn không thay đổi : gọi chuỗi kết nối "SecurePassDataBase"
string connectionString = ConfigurationManager.ConnectionStrings["SecurePassDataBase"].ToString();
- Mọi thắc mắc vui lòng liên hệ : Mr Thạnh (0918 074 869)
(Admin thiendac.vn)