Exemple #1
0
        protected virtual void HandleUnauthorizedRequest(
            AuthorizationContext filterContext,
            MethodInfo methodInfo,
            CodeZeroAuthorizationException ex)
        {
            filterContext.HttpContext.Response.StatusCode =
                filterContext.RequestContext.HttpContext.User?.Identity?.IsAuthenticated ?? false
                    ? (int)HttpStatusCode.Forbidden
                    : (int)HttpStatusCode.Unauthorized;

            var isJsonResult = MethodInfoHelper.IsJsonResult(methodInfo);

            if (isJsonResult)
            {
                filterContext.Result = CreateUnAuthorizedJsonResult(ex);
            }
            else
            {
                filterContext.Result = CreateUnAuthorizedNonJsonResult(filterContext, ex);
            }

            if (isJsonResult || filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
            }

            _eventBus.Trigger(this, new CodeZeroHandledExceptionData(ex));
        }
Exemple #2
0
 protected virtual CodeZeroJsonResult CreateUnAuthorizedJsonResult(CodeZeroAuthorizationException ex)
 {
     return(new CodeZeroJsonResult(
                new AjaxResponse(_errorInfoBuilder.BuildForException(ex), true))
     {
         JsonRequestBehavior = JsonRequestBehavior.AllowGet
     });
 }
Exemple #3
0
 protected virtual HttpStatusCodeResult CreateUnAuthorizedNonJsonResult(AuthorizationContext filterContext, CodeZeroAuthorizationException ex)
 {
     return(new HttpStatusCodeResult(filterContext.HttpContext.Response.StatusCode, ex.Message));
 }