Mas*_*oud 6 c# encryption entity-framework app-config winforms
我使用以下方法加密我的项目中的connectionstrings部分(我app.config在我的WinForms项目中使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
我也使用以下方法解密该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();
}
Run Code Online (Sandbox Code Playgroud)
这个方法适用于我的机器,但当我将我的应用程序部署到另一台机器时,我得到以下异常:
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 provider)
在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)
---内部异常堆栈跟踪结束---
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String [] keys,SectionInput input,Boolean isTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult,Boolean getLkg,Boolean getRuntimeObject,Object&result,Object&resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject)
在System.Configuration.Configuration.GetSection(String sectionName)
在IASCo.Infrastructure.Common.Utilities.Configuration.ConfigurationEncryption.DecryptConfig(string exeConfigName)
您需要使用解密的部分进行发布.用于加密/解密的密钥是特定于机器的.
我的应用程序将安装在网络共享上并从那里运行,但是有多个人可以从他们的工作站访问该应用程序,如何指定一个密钥来解密connectionString将使用所有机器的部分访问该应用程序.
我正在寻找一种方法来完成这项工作(在我的机器中加密并在用户的机器上解密)使用c#.
小智 1
你的代码对我来说看起来很好 - 除了需要从用户那里进行更改
\n\n if (section != null || !section.SectionInformation.IsProtected)\n{\n section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");\n config.Save();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n到
\n\n if (section != null || !section.SectionInformation.IsProtected)\n {\n section.SectionInformation.ProtectSection("RsaProtectionConfigurationProvider");\n config.Save();\n }\nRun Code Online (Sandbox Code Playgroud)\n\n创建 RSA 密钥时,请确保使用 -exp 开关按照文档启用密钥导出:https://msdn.microsoft.com/en-us/library/yxw286t2.aspx
\n\naspnet_regiis -pc "KeysetName"\xe2\x80\x93exp\nRun Code Online (Sandbox Code Playgroud)\n\n正如前面的回答者所说。除此之外,如果应用程序的用户位于 IIS 网络上,您可以通过组织访问控制列表 (ACL) 使用 ASP.NET 模拟。这将使您无需在计算机级别进行身份验证,这并不适合所有应用程序。请参阅: https: //msdn.microsoft.com/en-us/library/xh507fc5.aspx
\n