title: Server cannot set status after HTTP headers have been sent with MVC custom Authorize Attribute
body: <p>I am using ASP.NET MVC5 with my custom AuthorizeAttribute as below </p>
  
  <pre><code>protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
      {
          var httpContext = filterContext.HttpContext;
          var request = httpContext.Request;
          var response = httpContext.Response;
          var user = httpContext.User;
  
          if (request.IsAjaxRequest())
          {
              if (user.Identity.IsAuthenticated == false)
              {
                  response.StatusCode = (int)HttpStatusCode.Unauthorized;
                  response.StatusDescription = "You must be Login to process this request!";
              }
              else
              {
                  response.StatusCode = (int)HttpStatusCode.Forbidden;
                  response.StatusDescription = "Permission Denied! Please contact your Administrator";
              }
  
              response.SuppressFormsAuthenticationRedirect = true;
              response.End();
          }
  
          base.HandleUnauthorizedRequest(filterContext);
      }
  </code></pre>
  
  <p>When i am trying to send a responce code of 403 from above code i always got an exception from Application_Error method of Global.asax.cs having </p>
  
  <blockquote>
    <p>System.Web.HttpException (0x80004005): Server cannot set status after HTTP headers have been sent.</p>
  </blockquote>
  
  <p>Below is the Stack trace</p>
  
  <blockquote>
    <p>System.Web.HttpException (0x80004005): Server cannot set status after HTTP headers have been sent.
       at System.Web.HttpResponse.set_StatusCode(Int32 value)
       at System.Web.HttpResponseWrapper.set_StatusCode(Int32 value)
       at DevOdeeERP.Web.Support.SharedMVC.DodBaseController.OnException(ExceptionContext filterContext)
       at System.Web.Mvc.Controller.System.Web.Mvc.IExceptionFilter.OnException(ExceptionContext filterContext)
       at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList<code>1 filters, Exception exception)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult</code>1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase<code>1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase<code>1.End()
       at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.&lt;BeginExecute&gt;b__15(IAsyncResult asyncResult, Controller controller)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase<code>1.End()
       at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
       at System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)</p>
  </blockquote>