2012-09-24 37 views
0

這是Cache password for SQL Server connection as a hash的後續操作。鎖定時無法保護配置

http://www.codeproject.com/Articles/15392/Implementing-Protected-Configuration-With-Windows描述了本地Windows應用程序使用受保護配置的方法。但是,基於我的應用程序設計運行的環境,安裝程序是不可取的。

我試着運行以下命令:

 object settings = ConfigurationManager.GetSection("userSettings/MyApp.Properties.Settings"); 
     SectionInformation info = ((ConfigurationSection)settings).SectionInformation; 
     if (!info.IsProtected) 
      info.ProtectSection("DPAPIProtection"); 

在我的WPF應用程序的幾個不同的時間,但每次我這樣做的時候,我從.NET的SectionInformation.cs此異常VerifyIsEditable:

if (IsLocked) { 
    throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_when_locked)); 

所以。有沒有辦法(a)在加載並鎖定配置之前運行ProtectSection(),或者(b)在調用Settings.Default.Save()後,在應用程序結束時刷新配置,關閉並解鎖它,然後調用ProtectSection()

回答

0

固定它(雖然我不知道爲什麼,到底):

 Configuration conf = ConfigurationManager.OpenExeConfiguration 
      (ConfigurationUserLevel.PerUserRoamingAndLocal); 
     ClientSettingsSection settings = (ClientSettingsSection) 
      conf.GetSection("userSettings/MyApp.Properties.Settings"); 
     SectionInformation info = settings.SectionInformation; 

     if (!info.IsProtected) 
     { 
      info.ProtectSection("DPAPIProtection"); 
      info.ForceSave = true; 
      conf.Save(); 
     } 
相關問題