title: HandleUnauthorizedRequest goes in InfiniteLoop
body: <p>I have following two actions that keeps calling each other and goes in infinite loop. what am i doing wrong?</p>
  
  <pre><code>Public Overrides Sub OnAuthorization(filterContext As System.Web.Mvc.AuthorizationContext)
          'This calls the AuthorzeCore function and also makes sure that the browser does not cache this function
          MyBase.OnAuthorization(filterContext)
          If Not IsNothing(filterContext.Result) Then
              Return
          End If
          'Gets the calling Controller
          Dim controllerName As String = filterContext.Controller.GetType().Name
          'Gets the calling action
          Dim actionName As String = filterContext.ActionDescriptor.ActionName
  
          'Checks whether the logged in user has access to the action of the controller
          Dim canAccess As test.Security.Permissions.PermissionTypes
          canAccess = test.ApplicationSecurity.GetSecurityObject().GetAccess(controllerName &amp; "." &amp; actionName)
          If canAccess = Security.Permissions.PermissionTypes.DISABLE Then
              'User has access to the application but not to the action they are trying to access, so throw a Unauthorised exception
              filterContext.HttpContext.Response.StatusCode = 403
              HandleUnauthorizedRequest(filterContext)
          End If
  
      End Sub
  
      Protected Overrides Sub HandleUnauthorizedRequest(filterContext As System.Web.Mvc.AuthorizationContext)
          ''To make sure that we throw a not authorised error rather not authenticated message
          'If filterContext.HttpContext.Request.IsAuthenticated Then
          '    'filterContext.Result = New HttpStatusCodeResult(CType(System.Net.HttpStatusCode.Forbidden, Int32))
          '    filterContext.Result = New RedirectToRouteResult(
          'Else
          '    MyBase.HandleUnauthorizedRequest(filterContext)
          'End If
          If (filterContext.HttpContext.Request.IsAjaxRequest()) Then
              Dim urlHelper As UrlHelper = New UrlHelper(filterContext.RequestContext)
              filterContext.Result = New JsonResult With {.Data = New With {.Error = "NotAuthorized", .URL = urlHelper.Action("UnAuthorized", "Error")}, _
                                                          .JsonRequestBehavior = JsonRequestBehavior.AllowGet}
          ElseIf filterContext.HttpContext.Response.StatusCode = 403 Then
              filterContext.Result = New ViewResult With {.ViewName = "UnAuthorized"}
          Else
              filterContext.Result = New ViewResult With {.ViewName = "UnAuthenticated"}
  
          End If
      End Sub
  </code></pre>