无法更新和保存 web.config 文件
Unable to update and save web.config file
我试图加密 ASP.Net Web 应用程序的 web.config 文件的连接字符串部分
通过一个简单的 winforms 应用程序。
我运行 VS2010 以管理员身份运行 下面一段代码。
private void encryptButton_Click(object sender, EventArgs e)
{
Configuration config = GetConfiguration();
ConfigurationSection configSection = config.GetSection("connectionStrings");
if (configSection != null)
// Only encrypt the section if it is not already protected
if (!configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
configSection.SectionInformation.ForceSave = true;
config.Save();
DisplayWebConfig();
}
}
private Configuration GetConfiguration()
{
var configFile = new FileInfo(@"C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC");
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName,true,configFile.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/",vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}
因为我只有物理路径,所以我尝试将路径相应地映射到虚拟路径。但是 config.Save() 向我抛出以下错误
ConfigurationErrorsException was unhandled.
Unable to save config to file 'C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC'.
为了清楚起见,这就是 StackTrace 的样子
at System.Configuration.Internal.WriteFileContext.ReplaceFile(String Source, String Target)
at System.Configuration.Internal.WriteFileContext.Complete(String filename, Boolean success)
at System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.UpdateConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save()
at EncryptionandDecryptionApplication.Form1.encryptButton_Click(Object sender, EventArgs e) in C:\Users\abalawan\Desktop\CN\EncryptionandDecryptionApplication\EncryptionandDecryptionApplication\Form1.cs:line 55
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EncryptionandDecryptionApplication.Program.Main() in C:\Users\abalawan\Desktop\CN\EncryptionandDecryptionApplication\EncryptionandDecryptionApplication\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
你的代码很完美。似乎存在权限问题。暂时尝试向所有人提供对您的文件位置的完全访问权限。如果它有效那么它的权限问题(虽然给每个人完全权限不是一个好主意)
如果您想在您的应用程序中添加 UAC 权限,那么您可以添加 app.manifest 文件并在该文件中更改 requestedExecutionLevel 标签,如下所示:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
我试图加密 ASP.Net Web 应用程序的 web.config 文件的连接字符串部分 通过一个简单的 winforms 应用程序。 我运行 VS2010 以管理员身份运行 下面一段代码。
private void encryptButton_Click(object sender, EventArgs e)
{
Configuration config = GetConfiguration();
ConfigurationSection configSection = config.GetSection("connectionStrings");
if (configSection != null)
// Only encrypt the section if it is not already protected
if (!configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
configSection.SectionInformation.ForceSave = true;
config.Save();
DisplayWebConfig();
}
}
private Configuration GetConfiguration()
{
var configFile = new FileInfo(@"C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC");
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName,true,configFile.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/",vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}
因为我只有物理路径,所以我尝试将路径相应地映射到虚拟路径。但是 config.Save() 向我抛出以下错误
ConfigurationErrorsException was unhandled.
Unable to save config to file 'C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC'.
为了清楚起见,这就是 StackTrace 的样子
at System.Configuration.Internal.WriteFileContext.ReplaceFile(String Source, String Target)
at System.Configuration.Internal.WriteFileContext.Complete(String filename, Boolean success)
at System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.UpdateConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save()
at EncryptionandDecryptionApplication.Form1.encryptButton_Click(Object sender, EventArgs e) in C:\Users\abalawan\Desktop\CN\EncryptionandDecryptionApplication\EncryptionandDecryptionApplication\Form1.cs:line 55
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EncryptionandDecryptionApplication.Program.Main() in C:\Users\abalawan\Desktop\CN\EncryptionandDecryptionApplication\EncryptionandDecryptionApplication\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
你的代码很完美。似乎存在权限问题。暂时尝试向所有人提供对您的文件位置的完全访问权限。如果它有效那么它的权限问题(虽然给每个人完全权限不是一个好主意)
如果您想在您的应用程序中添加 UAC 权限,那么您可以添加 app.manifest 文件并在该文件中更改 requestedExecutionLevel 标签,如下所示:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />