使用provider 'DataProtectionConfigurationProvider'解密失败;
本文关键字:失败 解密 DataProtectionConfigurationProvider provider 使用 | 更新日期: 2023-09-27 18:08:14
我使用以下方法在我的WinForms
项目中加密我的app.config
的connectionstrings
部分(我在我的项目中使用Code First EF):
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();
}
}
,我也使用以下方法来解密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();
}
这个方法在我的机器上工作,但是当我将我的应用程序部署到另一台机器时,我得到以下异常:
System.Configuration。使用提供程序"DataProtectionConfigurationProvider"解密失败。来自提供程序的错误消息:密钥不能在指定状态下使用。(来自HRESULT: 0x8009000B的异常)(D:'l4test'Level4UI.exe)配置行82)——>System.Runtime.InteropServices.COMException:在指定状态下使用密钥无效。(Exception from HRESULT: 0x8009000B)
在System.Runtime.InteropServices.Marshal。ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
在System.Runtime.InteropServices.Marshal。ThrowExceptionForHR (Int32 errorCode)
在System.Configuration.DpapiProtectedConfigurationProvider。DecryptText(字符串encText)
在System.Configuration.DpapiProtectedConfigurationProvider。解密(XmlNode encryptedNode)
在System.Configuration.ProtectedConfigurationSection。解密部分(字符串encryptedXml, ProtectedConfigurationProvider provider)
在System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost。DecryptSection(String encryptedXml, ProtectedConfigurationProvider, ProtectedConfigurationSection protectedConfigSection)
在System.Configuration.Internal.DelegatingConfigHost。DecryptSection(String encryptedXml, ProtectedConfigurationProvider, ProtectedConfigurationSection protectedConfigSection)
在System.Configuration.Internal.DelegatingConfigHost。DecryptSection(String encryptedXml, ProtectedConfigurationProvider, ProtectedConfigurationSection protectedConfigSection)
在System.Configuration.BaseConfigurationRecord。CallHostDecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfig)
在System.Configuration.BaseConfigurationRecord。解密configsection (ConfigXmlReader reader, ProtectedConfigurationProvider protectionProvider)
——内部异常堆栈跟踪结束——
在System.Configuration.BaseConfigurationRecord。EvaluateOne(String[] keys, SectionInput input, Boolean istrust, FactoryRecord FactoryRecord, SectionRecord SectionRecord, objectparentresult)
在System.Configuration.BaseConfigurationRecord。评估(FactoryRecord FactoryRecord, SectionRecord SectionRecord,对象parentResult,布尔getLkg,布尔gettruntimeobject,对象&因此,Object&resultRuntimeObject)
在System.Configuration.BaseConfigurationRecord。GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,布尔requestIsHere, Object&因此,Object&resultRuntimeObject)
在System.Configuration.Configuration。GetSection(字符串sectionName)
在IASCo.Infrastructure.Common.Utilities.Configuration.ConfigurationEncryption。DecryptConfig(字符串exeConfigName)
在这个帖子里,Jeremy说:
您需要发布解密的部分。用于加密/解密的密钥是特定于机器的。
我的应用程序将安装在网络共享上并从那里运行,但是会有不止一个人可以从他们的工作站访问该应用程序,我如何指定一个密钥来解密connectionString
部分,该部分将从用于访问该应用程序的所有机器上工作。
我正在寻找一种方法来做这个工作(加密在我的机器和解密在用户的机器)使用c#。
你的代码看起来很好-除了需要从
的用户更改 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开关来启用导出密钥,如docs: 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