【问题标题】:ASP.NET security exception with OpenWebConfiguration on shared host共享主机上的 OpenWebConfiguration 的 ASP.NET 安全异常
【发布时间】:2010-10-13 19:39:15
【问题描述】:

将我的网站从本地开发环境移动到共享主机后,我得到:

Security Exception 

Description: The application attempted to perform an operation not allowed by 
the security policy.  To grant this application the required permission please
contact your system administrator or change the application's trust level in 
the configuration file.

问题出现在我的 web 应用程序中,任何地方都调用了以下代码:

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)

由于我的 Web 应用程序只是试图打开它自己的 web.config 文件,我不知道为什么这会被标记为安全异常。也许有人可以解释......但更重要的是我需要一个解决方案,我通过谷歌找到的几个解决方案很痛苦。

一个解决方案(来自许多帖子)说将信任级别配置为完全,但我被告知这在我的共享主机上是不可能的。

另一个解决方案(来自http://www.4guysfromrolla.com/articles/100307-1.aspx)说不使用 OpenWebConfiguration(),但我需要使用它来使用 DPAPI 加密配置部分(例如 connectionStrings)(有关更多信息,请参阅http://www.4guysfromrolla.com/articles/021506-1.aspx)。

请告知为什么我的 Web 应用程序上的 IIS barfs 试图打开它自己的 web.config,以及能够使用 DPAPI 加密部分 web.config 的解决方法。

【问题讨论】:

  • 我刚刚发现一个旧的堆栈溢出帖子 stackoverflow.com/questions/1358181/…> 表明 DPAPI 需要完全信任,这对于使用机器级密钥是有意义的。但是,我仍然想能够在我自己的 web.config(每个 Request.ApplicationPath)上使用 OpenWebConfiguration(),以便轻松访问 appSettings 部分。这真的需要完全信任吗?

标签: asp.net security iis web-config


【解决方案1】:

我过去曾遇到过这个问题。 OpenWebConfiguration() 方法还读取machine.config 文件。在部分信任且没有正确权限的情况下,您无法使用此方法。

如果您在 Visual Studio 2008/2010 中使用调试器进入 .NET Framework 程序集,您可以确切地看到正在发生的事情。

以下是进入WebConfigurationManager.OpenWebConfiguration()时捕获的调用堆栈:

mscorlib.dll!System.IO.FileStream.Init(string path = "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\Config\\machine.config", System.IO.FileMode 模式= 打开,System.IO.FileAccess 访问 = 读取,int 权限 = 0,bool useRights = false,System.IO.FileShare 共享 = 读取,int bufferSize = 4096,System.IO.FileOptions 选项 = 无,Microsoft.Win32.Win32Native .SECURITY_ATTRIBUTES secAttrs = null, string msgPath = "machine.config", bool bFromProxy = false) 第 326 行 C# mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) Line 259 C# System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.StaticOpenStreamForRead(string streamName) + 0x56 bytes System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(string streamName, bool assertPermissions) + 0x7d 字节 System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(string streamName) + 0xb 字节 System.Configuration.dll!System.Configuration.Internal.DelegatingConfigHost.OpenStreamForRead(string streamName) + 0xe 字节 System.Configuration.dll!System.Configuration.UpdateConfigHost.OpenStreamForRead(string streamName) + 0x2f 字节 System.Configuration.dll!System.Configuration.BaseConfigurationRecord.InitConfigFromFile() + 0x126 字节 System.Configuration.dll!System.Configuration.BaseConfigurationRecord.Init(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.BaseConfigurationRecord parent, string configPath, string locationSubPath) + 0xaa5 字节 System.Configuration.dll!System.Configuration.MgmtConfigurationRecord.Init(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.Internal.IInternalConfigRecord parent, string configPath, string locationSubPath) + 0x39 bytes System.Configuration.dll!System.Configuration.MgmtConfigurationRecord.Create(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.Internal.IInternalConfigRecord parent, string configPath, string locationSubPath) + 0x2a bytes System.Configuration.dll!System.Configuration.Internal.InternalConfigRoot.GetConfigRecord(string configPath) + 0x12d 字节 System.Configuration.dll!System.Configuration.Configuration.Configuration(string locationSubPath, System.Type typeConfigHost, object[] hostInitConfigurationParams) + 0xfd 字节 System.Configuration.dll!System.Configuration.Internal.InternalConfigurationFactory.System.Configuration.Internal.IInternalConfigConfigurationFactory.Create(System.Type typeConfigHost, object[] hostInitConfigurationParams) + 0x1e 字节 System.Web.dll!System.Web.Configuration.WebConfigurationHost.OpenConfiguration(System.Web.Configuration.WebLevel webLevel, System.Configuration.ConfigurationFileMap fileMap, System.Web.VirtualPath path, string site, string locationSubPath, string server, string userName , 字符串密码, System.IntPtr tokenHandle) 第 862 行 C# System.Web.dll!System.Web.Configuration.WebConfigurationManager.OpenWebConfigurationImpl(System.Web.Configuration.WebLevel webLevel, System.Configuration.ConfigurationFileMap fileMap, string path, string site, string locationSubPath, string server, string userName, string password, System.IntPtr userToken) 第 77 行 + 0x1c 字节 C# System.Web.dll!System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(string path) Line 140 + 0x25 bytes C#

不幸的是,您唯一的选择是使用功能不丰富的WebConfigurationManager.GetSection()

关于加密连接字符串。遗憾的是,此功能需要完全信任,没有其他方法可以解决。

【讨论】:

  • 我还能够使用 ConfigurationManager.AppSettings().Get("keyname"),因此我可以通过最少的工作获得应用程序设置。
猜你喜欢
  • 2011-09-04
  • 2013-01-15
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
相关资源
最近更新 更多