Note that there are some explanatory texts on larger screens.

plurals
  1. PO"cannot set status after HTTP headers" when overriding AuthorizeAttribute.AuthorizeCore
    primarykey
    data
    text
    <p>I'm trying to implement custom basic authentication on specific controller of MVC 2 application. Specifically I inherited from <code>AuthorizeAttribute</code> and override <code>AuthorizeCore()</code> method:</p> <pre><code>protected override bool AuthorizeCore(HttpContextBase httpContext) { if( doAuthorization() ) { return true; } // send HTTP 401 HttpContext context = HttpContext.Current; context.Response.StatusCode = 401; context.Response.AddHeader( "WWW-Authenticate", String.Format("Basic realm=\"{0}\"", myRealm); context.Response.End(); return false; } </code></pre> <p>and marked the controller with my inherited attribute.</p> <p>The whole thing works, but whenever <code>AuthorizeCore</code> returns <code>false</code> MVC continues processing the request and <code>Application_Error()</code> is invoked and I retrieve the following exception there:</p> <pre><code>Server cannot set status after HTTP headers have been sent. at System.Web.HttpResponse.set_StatusCode(Int32 value) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__4() at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) </code></pre> <p>How do I make MVC stop processing the request and prevent that exception?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload