10

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

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

第二个和第三个应用程序有点吐出以下SecurityException :随机:

替代文字

关联

异常文本:

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

4

1 回答 1

20

所以原来上面SecurityException的原因是3-fold

  • 我的 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时重新启动。这就是为什么我无法弄清楚问题出在哪里。

  • 我必须在web.config的 log4net configSection 中添加一个requirePermission="false"

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

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

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

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

于 2010-02-09T13:10:48.180 回答