2010-02-05 154 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