Encrypt App.Config user element with cmd

I can configure encryption of connection strings using the aspnet_regiis.exe command. Now I have created a configuration section to which the Custom Custom Element Collection is added, and this will save the value of the connection information.

 namespace ExpressSnapSortCreation { /// <summary> /// This Class hold the the Collection of Cofigration key /// </summary> internal class ServerReplicationsCollection : ConfigurationElementCollection { /// <summary> /// This Will return the ConfigurationElement /// </summary> /// <returns>ConfigurationElement</returns> protected override ConfigurationElement CreateNewElement() { return new ServerReplicationsElement(); } /// <summary> /// Get Element BY key /// </summary> /// <param name="element"></param> /// <returns></returns> protected override object GetElementKey(ConfigurationElement element) { return ((ServerReplicationsElement)element).Name; } /// <summary> /// This is override on the Elements /// </summary> public class ServerReplicationsElement : ConfigurationElement { /// <summary> /// Name of the Element /// </summary> [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } /// <summary> /// Data base name /// </summary> [ConfigurationProperty("connectionString", IsRequired = true)] public string ConnectionString { get { return (string)this["connectionString"]; } set { this["connectionString"] = value; } } /// <summary> /// Data base user name /// </summary> [ConfigurationProperty("providerName", IsRequired = true)] public string ProviderName { get { return (string)this["providerName"]; } set { this["providerName"] = value; } } /// <summary> /// Display Order /// </summary> [ConfigurationProperty("order", IsRequired = false)] public int Order { get { return (int)this["order"]; } set { this["order"] = value; } } } } } 

This is the partition creation code

  class ServerReplications : ConfigurationSection { /// <summary> /// The name of this section in the app.config. /// </summary> public const string SectionName = "ReplicationConfigurationSection"; /// <summary> /// Replication data base name /// </summary> private const string ReplicationCenterCollectionName = "ReplicationDataBases"; [ConfigurationProperty(ReplicationCenterCollectionName)] [ConfigurationCollection(typeof(ServerReplicationsCollection), AddItemName = "add")] public ServerReplicationsCollection ReplicationDataBases { get { return (ServerReplicationsCollection)base[ReplicationCenterCollectionName]; } } } 

This is my application configuration file.

  <?xml version="1.0"?> <configuration> <configSections> <section name="ReplicationConfigurationSection" type="ExpressSnapSortCreation.ServerReplications, ExpressSnapSortCreation" /> </configSections> <ReplicationConfigurationSection> <ReplicationDataBases> <add name="ApplicationServices" connectionString="Data Source=PC-002\SQLEXPRESS2014;Initial Catalog=AML25;Persist Security Info=True;User ID=sa; Password=StItS!@ #SeRvErPC-003" providerName="System.Data.SqlClient" order="1" /> <add name="ApplicationServices2" connectionString="Data Source=PC-004\SQLEXPRESS2014;Initial Catalog=AML26;Persist Security Info=True;User ID=sa; Password=StItS!@ #SeRvErPC-002" providerName="System.Data.SqlClient" order="2" /> </ReplicationDataBases> </ReplicationConfigurationSection> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> </startup> </configuration> 

In the application, we get the value of the connection string. Due to a security purpose, we cannot display data in the App.config file. then you need to encrypt the section below

  • This is the first command I used.

    aspnet_regiis.exe -pef "ReplicationConfigurationSection" "C:\Users\mukesh.singh\Documents\Visual Studio 2015\Projects\AML\ExpressSnapSortCreation"

Got Error Converting the file name "app.config" to "Web.config"

An error occurred while creating the configuration section handler for ReplicationConfigurationSection: the file or assembly "ExpressSnapSortCreation" or one of its dependencies could not be loaded. The system cannot find the specified file. (C: \ Users \ mukesh.singh \ Documents \ Visual Studio 2015 \ Projects \ AML \ ExpressSnapSortCreation \ bin \ Debug \ web.config line 4)

Failed to load file or assembly "ExpressSnapSortCreation" or one of its dependencies. The system cannot find the specified file. Error!

  1. After change

Failed to load type 'ExpressSnapSortCreation.ServerReplications' from assembly' System.Web, Version = 4.0.0.0, Culture = Neutral, PublicKeyToken = b03f5f7f11d50a3a ".

  1. I also tried this combination

    aspnet_regiis.exe -pef "ExpressSnapSortCreation.ServerReplications/ExpressSnapSortCreations" "C:\Users\mukesh.singh\Documents\Visual Studio 2015\Projects\AML\ExpressSnapSortCreation

0
source share
1 answer

This is the code I used to encrypt my custom section in app.config. I just open the app.config file inside the encrypted bin folder

  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection("ReplicationConfigurationSection"); if (section != null) { if (!section.IsReadOnly()) { if (!section.SectionInformation.IsProtected) { if (!section.ElementInformation.IsLocked) { section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); Console.WriteLine("Section {0} is now protected by {1}", section.SectionInformation.Name.ToString(), section.SectionInformation.ProtectionProvider.Name.ToString()); } } } } 

When I open the Config file, it looks like

  <configuration> <configSections> <section name="ReplicationConfigurationSection" type="ExpressSnapSortCreation.ServerReplications, ExpressSnapSortCreation" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" /> </configSections> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> </startup> <ReplicationConfigurationSection configProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue>FXUE9iChoq/7HGE4nV3muaPZy4ejcDCcZx0PVasHZJi4xRs0ZPXI08unUegvXs+C2FALEskpHa+Tt4u24I8OhSRS9QI+I2kpgxTlQvMFmsvFu6pkDQS1jt13EHmov0Thr1CBGhMXyHMm0EGr0+yWKI3PfD9vwGmQl0yawLdyiockQk9kCuik8g8jnpiyaidYL/RKpdwNPBuH9wOm8WWTXlUL4N+SO98jAX0PPoDjaDbDdB14t71Favg7vxpjIj5pDlljj59ek3pudW0etIHm6v8YsJaE9Et62DfzB31W4kmGNgmmGWTu4/hF93J0kv9VgkmKTcdOmeXq2KHA2JCLKg==</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>JkWK1jpOzcbtX8k/SVvTVRxQKO0ylRyWQ8imAOaIz51e3JmdhqXgdH8p5unceEiLCH2PTZOBZMIgYW4hYILiGnUE4SBZ6PfFr2vGowXdg13K808uSAx9taFb0HfvubcSQ23nwoBuJJmKfYooZX73YRnRvxIFH5SJZZ+WB8mTlNgaAZ+JftaN2rAlpH/cei4gwPCR64PaTu5VDJflSj0WnF7ZD13c0I0ZpHtJs29u0XTkBnAsL5DGULZsAexn2+89uaLfNpr9K+AYW8477TelVpnHGsMGDSOYOlWNUylldjATKZ/sgzDU/gq29dV+9RO18xvCHLXWjKKiT4B7UOlp82/1D/ky3OlK6opCEIJbCStm0q8MrvSQksdPN/yJ+S0Tv3E8hD4Wmf6grJOBlMGesomickqOzEudc+3fRwQS4Paf+ca3NgAk5utI+piZNhNtAnA/XU1ozDD0Zv381xaMTOTNjBq35hplK8zuHBVg+bZkbilSd3L4x4QAEv1Ds9Kt5hyUZyUNMWoXUXk0qoOP8UbMdHvUGvjsvAFudvZ7ZxtntiARptTFeTfg3qcghDdoyzOYBK3Md2urstEVdsdj6z6/RqBFO4qGY6hQ/IvIq+7lgG2rDsGH3AJlRNSdb+YJYktqGut65kvqcrSR2CgtYoGWcsUneBkpzQ65Rb0d6jL2Qt7zfJg1aA2iv97N15+tPjFDUQbbYFBi2ubvq1/pc7s/odaSUNK2LCfctXFG32MbEndJk1rXreLencAH/KlO2iJzA6QujwcT/LYo6w97lVbkrZAWWgxnUmVKeq+OwS6AybaK/sIw5wxBFsouCNdt</CipherValue> </CipherData> </EncryptedData> </ReplicationConfigurationSection> </configuration> + Tt4u24I8OhSRS9QI + I2kpgxTlQvMFmsvFu6pkDQS1jt13EHmov0Thr1CBGhMXyHMm0EGr0 + yWKI3PfD9vwGmQl0yawLdyiockQk9kCuik8g8jnpiyaidYL / RKpdwNPBuH9wOm8WWTXlUL4N + SO98jAX0PPoDjaDbDdB14t71Favg7vxpjIj5pDlljj59ek3pudW0etIHm6v8YsJaE9Et62DfzB31W4kmGNgmmGWTu4 / hF93J0kv9VgkmKTcdOmeXq2KHA2JCLKg == </ CipherValue>  <configuration> <configSections> <section name="ReplicationConfigurationSection" type="ExpressSnapSortCreation.ServerReplications, ExpressSnapSortCreation" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" /> </configSections> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> </startup> <ReplicationConfigurationSection configProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue>FXUE9iChoq/7HGE4nV3muaPZy4ejcDCcZx0PVasHZJi4xRs0ZPXI08unUegvXs+C2FALEskpHa+Tt4u24I8OhSRS9QI+I2kpgxTlQvMFmsvFu6pkDQS1jt13EHmov0Thr1CBGhMXyHMm0EGr0+yWKI3PfD9vwGmQl0yawLdyiockQk9kCuik8g8jnpiyaidYL/RKpdwNPBuH9wOm8WWTXlUL4N+SO98jAX0PPoDjaDbDdB14t71Favg7vxpjIj5pDlljj59ek3pudW0etIHm6v8YsJaE9Et62DfzB31W4kmGNgmmGWTu4/hF93J0kv9VgkmKTcdOmeXq2KHA2JCLKg==</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>JkWK1jpOzcbtX8k/SVvTVRxQKO0ylRyWQ8imAOaIz51e3JmdhqXgdH8p5unceEiLCH2PTZOBZMIgYW4hYILiGnUE4SBZ6PfFr2vGowXdg13K808uSAx9taFb0HfvubcSQ23nwoBuJJmKfYooZX73YRnRvxIFH5SJZZ+WB8mTlNgaAZ+JftaN2rAlpH/cei4gwPCR64PaTu5VDJflSj0WnF7ZD13c0I0ZpHtJs29u0XTkBnAsL5DGULZsAexn2+89uaLfNpr9K+AYW8477TelVpnHGsMGDSOYOlWNUylldjATKZ/sgzDU/gq29dV+9RO18xvCHLXWjKKiT4B7UOlp82/1D/ky3OlK6opCEIJbCStm0q8MrvSQksdPN/yJ+S0Tv3E8hD4Wmf6grJOBlMGesomickqOzEudc+3fRwQS4Paf+ca3NgAk5utI+piZNhNtAnA/XU1ozDD0Zv381xaMTOTNjBq35hplK8zuHBVg+bZkbilSd3L4x4QAEv1Ds9Kt5hyUZyUNMWoXUXk0qoOP8UbMdHvUGvjsvAFudvZ7ZxtntiARptTFeTfg3qcghDdoyzOYBK3Md2urstEVdsdj6z6/RqBFO4qGY6hQ/IvIq+7lgG2rDsGH3AJlRNSdb+YJYktqGut65kvqcrSR2CgtYoGWcsUneBkpzQ65Rb0d6jL2Qt7zfJg1aA2iv97N15+tPjFDUQbbYFBi2ubvq1/pc7s/odaSUNK2LCfctXFG32MbEndJk1rXreLencAH/KlO2iJzA6QujwcT/LYo6w97lVbkrZAWWgxnUmVKeq+OwS6AybaK/sIw5wxBFsouCNdt</CipherValue> </CipherData> </EncryptedData> </ReplicationConfigurationSection> </configuration> + JftaN2rAlpH / cei4gwPCR64PaTu5VDJflSj0WnF7ZD13c0I0ZpHtJs29u0XTkBnAsL5DGULZsAexn2 + 89uaLfNpr9K + AYW8477TelVpnHGsMGDSOYOlWNUylldjATKZ / sgzDU / gq29dV + 9RO18xvCHLXWjKKiT4B7UOlp82 / 1D / ky3OlK6opCEIJbCStm0q8MrvSQksdPN / yJ + S0Tv3E8hD4Wmf6grJOBlMGesomickqOzEudc + 3fRwQS4Paf + ca3NgAk5utI + piZNhNtAnA / XU1ozDD0Zv381xaMTOTNjBq35hplK8zuHBVg + bZkbilSd3L4x4QAEv1Ds9Kt5hyUZyUNMWoXUXk0qoOP8UbMdHvUGvjsvAFudvZ7ZxtntiARptTFeTfg3qcghDdoyzOYBK3Md2urstEVdsdj6z6 / RqBFO4qGY6hQ / IvIq + 7lgG2rDsGH3AJlRNSdb + YJYktqGut65kvqcrSR2CgtYoGWcsUneBkpzQ65Rb0d6jL2Qt7zfJg1aA2iv97N15 + tPjFDUQbbYFBi2ubvq1  <configuration> <configSections> <section name="ReplicationConfigurationSection" type="ExpressSnapSortCreation.ServerReplications, ExpressSnapSortCreation" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" /> </configSections> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> </startup> <ReplicationConfigurationSection configProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue>FXUE9iChoq/7HGE4nV3muaPZy4ejcDCcZx0PVasHZJi4xRs0ZPXI08unUegvXs+C2FALEskpHa+Tt4u24I8OhSRS9QI+I2kpgxTlQvMFmsvFu6pkDQS1jt13EHmov0Thr1CBGhMXyHMm0EGr0+yWKI3PfD9vwGmQl0yawLdyiockQk9kCuik8g8jnpiyaidYL/RKpdwNPBuH9wOm8WWTXlUL4N+SO98jAX0PPoDjaDbDdB14t71Favg7vxpjIj5pDlljj59ek3pudW0etIHm6v8YsJaE9Et62DfzB31W4kmGNgmmGWTu4/hF93J0kv9VgkmKTcdOmeXq2KHA2JCLKg==</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>JkWK1jpOzcbtX8k/SVvTVRxQKO0ylRyWQ8imAOaIz51e3JmdhqXgdH8p5unceEiLCH2PTZOBZMIgYW4hYILiGnUE4SBZ6PfFr2vGowXdg13K808uSAx9taFb0HfvubcSQ23nwoBuJJmKfYooZX73YRnRvxIFH5SJZZ+WB8mTlNgaAZ+JftaN2rAlpH/cei4gwPCR64PaTu5VDJflSj0WnF7ZD13c0I0ZpHtJs29u0XTkBnAsL5DGULZsAexn2+89uaLfNpr9K+AYW8477TelVpnHGsMGDSOYOlWNUylldjATKZ/sgzDU/gq29dV+9RO18xvCHLXWjKKiT4B7UOlp82/1D/ky3OlK6opCEIJbCStm0q8MrvSQksdPN/yJ+S0Tv3E8hD4Wmf6grJOBlMGesomickqOzEudc+3fRwQS4Paf+ca3NgAk5utI+piZNhNtAnA/XU1ozDD0Zv381xaMTOTNjBq35hplK8zuHBVg+bZkbilSd3L4x4QAEv1Ds9Kt5hyUZyUNMWoXUXk0qoOP8UbMdHvUGvjsvAFudvZ7ZxtntiARptTFeTfg3qcghDdoyzOYBK3Md2urstEVdsdj6z6/RqBFO4qGY6hQ/IvIq+7lgG2rDsGH3AJlRNSdb+YJYktqGut65kvqcrSR2CgtYoGWcsUneBkpzQ65Rb0d6jL2Qt7zfJg1aA2iv97N15+tPjFDUQbbYFBi2ubvq1/pc7s/odaSUNK2LCfctXFG32MbEndJk1rXreLencAH/KlO2iJzA6QujwcT/LYo6w97lVbkrZAWWgxnUmVKeq+OwS6AybaK/sIw5wxBFsouCNdt</CipherValue> </CipherData> </EncryptedData> </ReplicationConfigurationSection> </configuration> 

More details.

+1
source

Source: https://habr.com/ru/post/895322/


All Articles