有區別地展示錯誤資訊

來源:互聯網
上載者:User
在asp.net 2.0中,當應用出現錯誤時,可以向使用者展示友好的出錯資訊,讓使用者看不到直接的出錯資訊和出錯的位置,以避免敏感的資訊泄露。但有時,如果想讓開發人員在遠程能看到具體的詳細開發資訊的話,則又要分開區別對待,Scott在他的BLOG裡教了大家如做了,現總結之(http://weblogs.asp.net/scottgu/default.aspx)

     首先,我們在web.config中設定如下
 <customErrors mode="RemoteOnly">

        <error statusCode="500" redirect="friendlyErrorPage.htm"/>

      </customErrors>

這裡friendlyErrorPage.htm為一個自訂製作的友善頁面
,為了能讓比如程式員或者其他許可權的人能看到比較詳細具體的出錯資訊,則需要在global.asax中的application_error事件中添加如下代碼
  Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

                      If (Context IsNot Nothing) And (Context.User.IsInRole("Developer")) Then

                        Dim err As Exception = Server.GetLastError()

                        Response.Clear()

                        Response.Write("<h1>" & err.InnerException.Message & "</h1>")

            Response.Write("<pre>" & err.ToString & "</pre>")

                        Server.ClearError()

                    End If

            End Sub

  這裡,首先判斷使用者訪問的角色,當發現是一個 developer角色時,則用Server.GetLastError()方法得出最新的異常資訊並顯示出來。
   而為了調試方便,可以改用
If (Context IsNot Nothing) And (Context.User.IsInRole("BUILTIN\Administrators")) Then
  來進行本機調試也可以看到啟動並執行效果,這裡判斷只有Administrators使用者才能看到具體出錯資訊

      如果要進一步學習如何在asp.net 2.0中實現角色管理,可以參考SCOTT的文章
http://weblogs.asp.net/scottgu/pages/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server.aspx

   
   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.