<p>I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually when a user tries to access a resource without proper authorization I want them to be redirected to a login page. FormsAuth does this for me easy enough.</p>

<p>Problem: Now I have an action being accessed by a console app. Whats the quickest way to have this action respond w/ status 401 instead of redirecting the request to the login page? </p>

<p>I want the console app to be able to react to this 401 StatusCode instead of it being transparent. I'd also like to keep the default, redirect unauthorized requests to login page behavior.</p>

<p>Note: As a test I added this to my global.asax and it didn't bypass forms auth:</p>

<pre><code>protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    HttpContext.Current.SkipAuthorization = true;
}
</code></pre>

<hr>

<p>@Dale and Andy</p>

<p>I'm using the AuthorizeAttributeFilter provided in MVC preview 4. This is returning an HttpUnauthorizedResult. This result is correctly setting the statusCode to 401. The problem, as i understand it, is that asp.net is intercepting the response (since its taged as a 401) and redirecting to the login page instead of just letting it go through. I want to bypass this interception for certain urls.</p>

[{'answer_id': 35341, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': -1, 'selected': False, 'text': "<p>I did some googling and this is what I came up with:</p>\n\n<pre><code>\n    HttpContext.Current.Response.StatusCode = 401;\n</code></pre>\n\n<p>Not sure if it works or not, I haven't tested it. Either way, it's worth a try, right? :)</p>\n"}, {'answer_id': 35352, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>Did you write your own FormsAuth attribute for the action?  If so, in the OnActionExecuting method, you get passed the FilterExecutingContext.  You can use this to pass back the 401 code.</p>\n\n<pre><code>public class FormsAuth : ActionFilterAttribute\n{\n    public override void OnActionExecuting(FilterExecutingContext filterContext)\n    {\n        filterContext.HttpContext.Response.StatusCode = 401;\n        filterContext.Cancel = true;\n    }\n}\n</code></pre>\n\n<p>This should work.  I am not sure if you wrote the FormsAuth attribute or if you got it from somewhere else.</p>\n'}, {'answer_id': 35415, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': "<p>I haven't used the AuthorizeAttribute that comes in Preview 4 yet.  I rolled my own, because I have been using the MVC framework since the first CTP.  I took a quick look at the attribute in reflector and it is doing what I mentioned above internally, except they use the hex equivalent of 401.  I will need to look further up the call, to see where the exception is caught, because more than likely that is where they are doing the redirect.  This is the functionality you will need to override.  I am not sure if you can do it yet, but I will post back when I find it and give you a work around, unless Haacked sees this and posts it himself.</p>\n"}, {'answer_id': 35516, 'author': "Dane O'Connor", 'author_id': 1946, 'author_profile': 'https://Stackoverflow.com/users/1946', 'pm_score': 4, 'selected': True, 'text': '<p>Ok, I worked around this. I made a custom ActionResult (HttpForbiddenResult) and custom ActionFilter (NoFallBackAuthorize).</p>\n\n<p>To avoid redirection, HttpForbiddenResult marks responses with status code 403. FormsAuthentication doesn\'t catch responses with this code so the login redirection is effectively skipped. The NoFallBackAuthorize filter checks to see if the user is authorized much like the, included, Authorize filter. It differs in that it returns HttpForbiddenResult when access is denied. </p>\n\n<p>The HttpForbiddenResult is pretty trivial:</p>\n\n<pre>\npublic class HttpForbiddenResult : ActionResult\n{\n    public override void ExecuteResult(ControllerContext context)\n    {\n        if (context == null)\n        {\n            throw new ArgumentNullException("context");\n        }\n        context.HttpContext.Response.StatusCode = 0x193; // 403\n    }\n}\n</pre>\n\n<p>It doesn\'t appear to be possible to skip the login page redirection in the FormsAuthenticationModule.</p>\n'}, {'answer_id': 35518, 'author': 'Duncan Smart', 'author_id': 1278, 'author_profile': 'https://Stackoverflow.com/users/1278', 'pm_score': 1, 'selected': False, 'text': '<p>Might be a kludge (and may not even work) but on your Login page see if <code>Request.QueryString["ReturnUrl"] != null</code> and if so set <code>Response.StatusCode = 401</code>.</p>\n\n<p>Bear in mind that you\'ll still need to get your console app to authenticate somehow. You don\'t get HTTP basic auth for free: you have to roll your own, but there are plenty of implementations about.</p>\n'}]
['https://Stackoverflow.com/questions/35322', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1946/']