首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用用户级DPAPI (WinForms)保护Config文件

使用用户级DPAPI (WinForms)保护Config文件
EN

Stack Overflow用户
提问于 2014-01-24 15:33:03
回答 1查看 749关注 0票数 1

我想保护我的app.config文件中的连接字符串。我是用这个代码来做的:

代码语言:javascript
复制
Public Shared Sub ProtectConnString()
    Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Dim configSection As System.Configuration.ConfigurationSection
    configSection = config.ConnectionStrings
    If Not (configSection Is Nothing) Then
        If Not (configSection.ElementInformation.IsLocked) Then
            configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
            configSection.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
        End If
    End If
End Sub

然而,我注意到它正在使用机器级的DPAPI。我想让它使用用户级的DPAPI。我怎样才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2014-05-21 19:46:35

如果您想使用用户级别的DataProtectionConfigurationProvider而不是机器级别,请将下面的配置添加到app.config中,然后添加如下所示的代码。

将此添加到app.config

代码语言:javascript
复制
<configProtectedData>
  <providers>
    <add useMachineProtection="false" keyEntropy="" name="MyUserDataProtectionConfigurationProvider" 
type="System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
</configProtectedData>

C#码

代码语言:javascript
复制
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            SectionInformation appSettingsSecInfo = config.GetSection("appSettings").SectionInformation;
            if (!appSettingsSecInfo.IsProtected)
            {
               appSettingsSecInfo.ProtectSection("MyUserDataProtectionConfigurationProvider");

                appSettingsSecInfo.ForceSave = true;

                config.Save(ConfigurationSaveMode.Full);
                MessageBox.Show("Config was not encrypted but now is encrypted");
            }
            else
            {
                MessageBox.Show("Config is already encrypted");
            }

MessageBox.Show("Some very secure information is about to be shown: " + ConfigurationManager.AppSettings["SomeImportantInfo"].ToString());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21336455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档