Asp.net
log4net 在 ASP.Net MVC Web 應用程序中拋出安全異常
我已經編寫了 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 應用程序位於完全不同的伺服器上。
所以原來上面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 中準確地找到了源異常的起源,並從那裡找出了問題所在。
