Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net UnauthorizedAccessException()类错误_Asp.net - Fatal编程技术网

Asp.net UnauthorizedAccessException()类错误

Asp.net UnauthorizedAccessException()类错误,asp.net,Asp.net,我有一个应用程序,它使用自己的授权来确定用户是否有权访问页面。如果访问被拒绝,我想显示一个更友好的“拒绝访问”页面。在母版页中 if (!authorize) { throw new UnauthorizedAccessException(); //error occurs here, looks like I'm not allowed to use this class } 在web.config中 <cu

我有一个应用程序,它使用自己的授权来确定用户是否有权访问页面。如果访问被拒绝,我想显示一个更友好的“拒绝访问”页面。在母版页中

        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.  

嗯,您正在抛出一个
未经授权的访问异常
。如果没有捕获它的
try catch
,代码将在那里崩溃。我认为你看到的异常就是你的异常。

正如弗雷德里克所说,你抛出了一个错误,所以你得到了一个错误。如果只想实例化异常,不要使用throw

UnauthorizedAccessException uae=新的UnauthorizedAccessException(“某些消息”)

但同样,这只是创造了一个例外;一旦你扔出它,你就会得到你已经得到的信息

为什么不直接重定向?Response.Redirect(“~/AccessDeniedPage.aspx”,False)

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

 /**************************************************************************************************************************
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. 
*************************************************************************************************************************/