2011-03-11 26 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

相關問題