2011-03-11 94 views
1

我有一个应用程序使用自己的授权来确定用户是否有权访问页面。我想在访问被拒绝的情况下显示更友好的“访问被拒绝”页面。在母版...UnauthorizedAccessException()类错误

 if (!authorize) 
     { 
      throw new UnauthorizedAccessException(); //error occurs here, looks like I'm not allowed to use this class 
     } 

在web.config

<customErrors mode="Off" defaultRedirect="~/ErrorPages/ErrorPage.aspx"> 
    <error statusCode="403" redirect="AccessDeniedPage.aspx" /> 
</customErrors>I get the error below. 

看来,我得到的错误一样的只是试图实例/使用UnauthorizedAccessException()类的结果。我想这样做,有没有办法使用它?

/************************************************************************************************************************** 
Attempted to perform an unauthorized operation. 

Exception Details: System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. 

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 
*************************************************************************************************************************/ 

回答

0

那么,你抛出UnauthorizedAccessException。如果没有捕获它的try-catch,代码将在那里崩溃。我认为你看到的例外是你的例外。

2

正如Fredrik所说,你正在抛出一个错误,所以你会得到一个错误。如果你想实例化异常,不要使用throw。

UnauthorizedAccessException uae = new UnauthorizedAccessException(“some message”);

但是,这只是创建一个异常;一旦你扔掉它,你会得到你已经得到的消息。

为什么不只是重定向? Response.Redirect(“〜/ AccessDeniedPage.aspx”,False);

如果您确实想要使用该异常,则可以继续按原样抛出异常,但也要处理Global.asax文件的Application_Error事件中的异常。在Application_Error事件中,测试异常是否为UnauthorizedAccessException,如果是,则将用户重定向到AccessDeniedPage.aspx。 Application_Error的基本使用:MSDN

相关问题