/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks ConnectionStrings.Encrypt/CS/source.cs
56 строк
2 KB
Bill Wagner
Update samples (#38401)
27 ноя 2023, 18:05
Не верифицирован
27 ноя 2023, 18:05
0eadf05
Код
Авторство
О чём код?
using System; using System.Configuration; static class Program { static void Main() { } // <Snippet1> static void ToggleConfigEncryption(string exeFile) { // Get the application path needed to obtain // the application configuration file. // Takes the executable file name without the // .config extension. var exePath = exeFile.Replace(".config", ""); try { // Open the configuration file and retrieve // the connectionStrings section. Configuration config = ConfigurationManager. OpenExeConfiguration(exePath); var section = config.GetSection("connectionStrings") as ConnectionStringsSection; if (section != null) { 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); } } // </Snippet1> }