代码之家  ›  专栏  ›  技术社区  ›  Saajid Ismail

log4net在ASP.NET MVC Web应用程序中引发安全异常

  •  10
  • Saajid Ismail  · 技术社区  · 15 年前

    我已经编写了3个ASP.NET MVC Web应用程序,它们都部署在与ISP共享的托管服务器上。

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

    第二和第三个应用程序指出了以下内容 安全例外 有点 随机:

    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应用程序位于完全不同的服务器上。

    1 回复  |  直到 8 年前
        1
  •  20
  •   Saajid Ismail    15 年前

    因此,上述安全例外的原因是3倍

    • 我的ISP已将ASP.NET配置为在 媒介信任 新服务器上的模式,以及 完全信任 旧服务器上的模式。我的Web应用程序在这两个服务器之间被拆分,这就是为什么我在应用程序之间获得不同的行为,即使它们的配置完全相同。

    • 我正在使用log4net进行错误日志记录,并且在我的 阿萨克斯全球公司 文件,我有以下信息:

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

      这条线- log4net.Config.XmlConfigurator.Configure(); 是引发上述异常的原因。它只在应用程序启动时发生一次,或者在应用程序启动时重新启动 Web.CONFIG 被修改。这就是为什么我不知道问题是从哪里来的。

    • 我不得不加上一个 requirePermission=“假” 到中的log4net配置部分 Web.CONFIG :

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

    如果我是在中等信任模式下发展的,我会发现这些问题。通过将以下内容添加到my web.config,可以强制应用程序以中等信任模式运行:

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

    通过强制我的应用程序以中等信任模式运行,我在dev中准确地找到了源异常的来源,并从中找出了问题所在。