Elmah框架,添加日誌文件訪問權限

    Elmah 簡述

    ELMAH(The Error Logging Modules And Handlers),直譯過來就是“錯誤日誌模塊和處理”,它提供了一個用於集中記錄和通知錯誤日誌的機制。它是專用於ASP.NET的完全可熱插拔的錯誤日誌記錄工具。其特點就是無需ASP.NET程序重新編譯,即可通過配置web.config(或machine.config)來實現整個應用程序甚至是IIS中所有ASP.NET應用程序的錯誤日誌記錄工作。它支持日誌的多種存儲方式(各種數據庫、XML、內存存儲),除了提供一個界面用於查詢日誌詳細信息外,還可以通過E-MAIL、RSS訂閱或Twitter發佈方式通知錯誤信息給相關人員。

 

    Elmah 配置

web.config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<configSections>
     <!-- Elmah日誌記錄插件 配置 -->
      <sectionGroup name="elmah">
          <section name="security" type="Elmah.SecuritySectionHandler, Elmah"/>
          <section name="errorLog" type="Elmah.ErrorLogSectionHandler, Elmah"/>
          <!-- <section name="errorMail" type="Elmah.ErrorMailSectionHandler, Elmah"/>
    <section name="errorFilter"  type="Elmah.ErrorFilterSectionHandler, Elmah"/> -->
      </sectionGroup>
  </configSections>
  <!-- Elmah日誌記錄插件 配置 -->
  <elmah>
      <!-- 是否允許遠程訪問。0代表否、1代表是
  <security allowRemoteAccess="1" /> -->
      <!-- 錯誤郵件發送 配置
    from: 用於發送的郵箱
    to: 發送到的終端郵箱地址,多個用','分隔
    subiect: 標題
    async: 是否異步方式
    smtpPort: SMTP端口
    smtpServer:  SMTP服務地址
    userName: 郵箱賬號
    password: 郵箱密碼
    noYsod: 郵件中是否包含附件
  -->
      <!-- <errorMail
      from="[email protected]"  
      subject="系統出錯...." 
      async="true"
      smtpPort="25"
      smtpServer="mail.test.com"
      userName="[email protected]"
      password="*****"
      noYsod="true|false" />
</elmah> -->
      <errorLog type="Elmah.AccessErrorLog, Elmah" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Elmah.mdb"/>
  </elmah>

 

請求處理配置:

1
2
3
4
5
6
<httpModules>
       <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
 </httpModules>
 <httpHandlers>
      <add verb="POST,GET,HEAD" path="webLog.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
</httpHandlers>

 

這個時候就能通過 ~/webLog.axd 這個請求URL 來讓Elmah框架處理,入口是Elmah.ErrorLogPageFactory !!!!!

這個時候因爲Elmah沒有做權限驗證,只要訪問 ~/webLog.axd 都能看到日誌信息,所以這樣很不安全。

解決方案1:在IIS配置URL訪問權限,只有服務器本機的指定帳號才能訪問該路徑。

解決方案2:

在web.config配置URL權限訪問限制:

1
2
3
4
5
6
7
<location path="webLog.axd">
    <system.web>
        <authorization>
            <deny users="?"/>
        </authorization>
    </system.web>
</location>

拒絕所有匿名用戶訪問webLog.axd

 

在Global.asax文件裏面Application_AuthenticateRequest (處理權限驗證促發)事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    // 判斷是否已經登錄
    if (Request.IsAuthenticated && !Request.IsStaticFile())
    {
        // IOC注入:登錄用戶服務實例
        var identityService = ServiceLocator.Current.GetInstance<IIdentityService>();
        // 分配當前上下文用戶
        Context.User = identityService.GetCurrentUser();
    }
    // 處理日誌權限問題
    if (Request.Url.ToString().Contains("webLog.axd"))
    {
        // 獲取當前訪問用戶
        var user = Context.User as UserPrincipal;
        // 業務邏輯,如果用戶不爲超級管理員則無法訪問該請求數據
        if (user == null || (user.UserRole & UserRole.SuperAdmin) == 0)
        {
           Response.Write("無權限訪問");
           Response.End();
        
    }
}

      完畢!!! 這樣只要訪問webLog.axd 時候都會促發權限驗證事件,這個時候就能進行訪問的權限控制...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章