在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​