2011-03-11 2 views
1

사용자가 페이지에 액세스 할 수 있는지 여부를 확인하기 위해 자체 권한을 사용하는 응용 프로그램이 있습니다. 액세스가 거부 된 경우 더 친숙한 "액세스 거부"페이지를 표시하려고합니다. Web.config의UnauthorizedAccessException() 클래스 오류

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

에서 MasterPage ...

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

에서 내가 그냥 단지 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