2010-02-05 106 views
10

我已经写了3个ASP.net MVC Web应用程序,并且都与我的ISP一起部署在共享主机服务器上。log4net在ASP.Net MVC Web应用程序中抛出安全异常

所有3个应用程序在配置和设置上都非常相似。第一个应用程序部署到第二个和第三个不同的服务器上。第一次申请没有给我任何错误。

的第二和第三应用程序吐出以下抛出:SecurityException有些:随机:

alt text http://i46.tinypic.com/24p9pac.jpg

Link

异常文本:

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. 

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Configuration.ConfigurationPermission, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[SecurityException: Request for the permission of type 'System.Configuration.ConfigurationPermission, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.] 
    System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0 
    System.Security.CodeAccessPermission.Demand() +58 
    System.Configuration.BaseConfigurationRecord.CheckPermissionAllowed(String configKey, Boolean requirePermission, Boolean isTrustedWithoutAptca) +99 


Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082 

我在部署或编辑web.config后第一次点击页面时出现上述错误。但是,在后续页面重新加载时,我不明白。这两个网站在今天的其余时间将再次罚款,但第二天早上我再次得到同样的错误。

在我编辑web.config后,错误确实出现,我假设它正在强制重新编译?

请帮忙。我不确定问题是什么。听起来像它与IIS中的安全设置有关。所有3个Web应用程序都以非常相似的方式进行设置和部署,不同之处在于第一个不出现错误的Web应用程序位于完全不同的服务器上。

回答

20

所以事实证明,对于上述抛出:SecurityException原因是3倍

  • 我的ISP已经ASP.net配置为中等信任模式下它的新服务器,并完全信任运行其旧服务器上的模式。我的Web应用程序是这2个服务器之间的分裂,这就是为什么我得到了应用程序之间的不同的行为,即使它们是完全相同的配置相同

  • 我使用log4net的错误日志记录,并在我的的Global.asax文件中,我有以下几点:

    protected void Application_Start() 
    { 
        RegisterRoutes(RouteTable.Routes); 
        log4net.Config.XmlConfigurator.Configure(); 
        log.Debug("Logging Initialized."); 
    } 
    

    这条线 - log4net.Config.XmlConfigurator.Configure();就是抛出上述异常。它只会在应用程序启动时发生,或者如果它们被重新启动web.config被修改。这就是为什么我无法弄清楚问题来自何处。

  • 我不得不添加一个requirePermission = “假”到log4net的configSection在web.config中

    <section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 
    

现在,如果我在中等信任模式正在发展,我会挑起这些问题。通过将以下内容添加到我的网站,您可以强制您的应用以中等信任模式运行。配置:

<system.web> 
    <trust level="Medium"/> 
    </system.web> 

通过迫使我的应用程序在中等信任模式下运行,我正好拿起在开发源例外,它起源,并推断出了什么从那里错了..

+3

优秀小费将信任级别设置为“中”,从而启用本地计算机上的调试。谢谢! – Adventure 2013-06-05 19:44:35