在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