有区别地展示错误信息

在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

 

http://www.cnblogs.com/jackyrong/archive/2006/08/17/479492.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值