Failed to decrypt using the DataProtectionConfigurationProvider provider in app.config

I used the following method to encrypt connectionstringsmy section app.configin my project WinForms(I use Code First EF in my project):

public static void EncryptConfig(string exeConfigName)
{
    var config = ConfigurationManager.OpenExeConfiguration(exeConfigName);
    var section = config.GetSection("connectionStrings") as ConnectionStringsSection;
    if (section != null || !section.SectionInformation.IsProtected)
    {
       section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
       config.Save();
    }
}

and I also use the following method to decrypt the partition connectionstrings:

public static void DecryptConfig(string exeConfigName)
{
    var config = ConfigurationManager.OpenExeConfiguration(exeConfigName);
    var section = config.GetSection("connectionStrings") as ConnectionStringsSection;
    if (section != null && section.SectionInformation.IsProtected)
        section.SectionInformation.UnprotectSection();
}

this method works on my machine, but when I deploy my application to another machine, I get the following exception:

System.Configuration.ConfigurationErrorsException: DataProtectionConfigurationProvider. : . ( HRESULT: 0x8009000B) (D:\l4test\Level4UI.exe.config 82) --- > System.Runtime.InteropServices.COMException: . ( HRESULT: 0x8009000B)

System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)

System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)

System.Configuration.DpapiProtectedConfigurationProvider.DecryptText(String encText)

System.Configuration.DpapiProtectedConfigurationProvider.Decrypt(XmlNode encryptedNode)

System.Configuration.ProtectedConfigurationSection.DecryptSection(String encryptedXml, ProtectedConfigurationProvider )

System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)

System.Configuration.Internal.DelegatingConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)

System.Configuration.Internal.DelegatingConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)

System.Configuration.BaseConfigurationRecord.CallHostDecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfig)

System.Configuration.BaseConfigurationRecord.DecryptConfigSection(ConfigXmlReader reader, ProtectedConfigurationProvider protectionProvider)

--- ---

System.Configuration.BaseConfigurationRecord.EvaluateOne(String [] , SectionInput , isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, parentResult)

System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, parentResult, Boolean, Boolean getLkg getRuntimeObject, Object & , & resultRuntimeObject)

System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String, Boolean configKey getLkg, checkPermission, getRuntimeObject, requestIsHere, Object & , & resultRuntimeObject)

System.Configuration.Configuration.GetSection(String sectionName)

IASCo.Infrastructure.Common.Utilities.Configuration.ConfigurationEncryption.DecryptConfig( exeConfigName)

, Jeremy :

. , /, .

, , . connectionString, , .

( ) #.

+4
1

-

 if (section != null || !section.SectionInformation.IsProtected)
{
   section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
   config.Save();
}

 if (section != null || !section.SectionInformation.IsProtected)
    {
       section.SectionInformation.ProtectSection("RsaProtectionConfigurationProvider");
       config.Save();
    }

RSA, , -exp : https://msdn.microsoft.com/en-us/library/yxw286t2.aspx

aspnet_regiis -pc "KeysetName"exp

. , IIS, ASP.NET (ACL) . , . .: https://msdn.microsoft.com/en-us/library/xh507fc5.aspx

0

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


All Articles