共享主机上具有OpenWebConfiguration的ASP.NET安全性异常

时间:2010-10-13 19:39:15

标签: asp.net security iis web-config

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

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文件,我不知道为什么这会被标记为安全异常。也许有人可以解释......但更重要的是我需要一个解决方案,我通过Google发现的几个解决方案很痛苦。

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

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

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

1 个答案:

答案 0 :(得分:0)

我过去曾经历过这个问题。 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 mode = Open, System.IO.FileAccess access = Read, int rights = 0, bool useRights = false, System.IO.FileShare share = Read, int bufferSize = 4096, System.IO.FileOptions options = None, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs = null, string msgPath = "machine.config", bool bFromProxy = false) Line 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 bytes 

System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(string streamName) + 0xb bytes 

System.Configuration.dll!System.Configuration.Internal.DelegatingConfigHost.OpenStreamForRead(string streamName) + 0xe bytes 

System.Configuration.dll!System.Configuration.UpdateConfigHost.OpenStreamForRead(string streamName) + 0x2f bytes 

System.Configuration.dll!System.Configuration.BaseConfigurationRecord.InitConfigFromFile() + 0x126 bytes 

System.Configuration.dll!System.Configuration.BaseConfigurationRecord.Init(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.BaseConfigurationRecord parent, string configPath, string locationSubPath) + 0xaa5 bytes 

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 bytes 

System.Configuration.dll!System.Configuration.Configuration.Configuration(string locationSubPath, System.Type typeConfigHost, object[] hostInitConfigurationParams) + 0xfd bytes 

System.Configuration.dll!System.Configuration.Internal.InternalConfigConfigurationFactory.System.Configuration.Internal.IInternalConfigConfigurationFactory.Create(System.Type typeConfigHost, object[] hostInitConfigurationParams) + 0x1e bytes 

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, string password, System.IntPtr tokenHandle) Line 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) Line 77 + 0x1c bytes C#

System.Web.dll!System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(string path) Line 140 + 0x25 bytes C#

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

关于加密连接字符串。可悲的是,这个功能要求完全信任,没有其他办法解决它。

相关问题