I am writing an application using asp.net-mvc for deployment in iis6. I use authentication. Usually, when a user tries to access a resource without proper authorization, I want them to be redirected to the login page. FormsAuth makes this easy enough for me.
Problem: I now have an action available by the console application. What is the fastest way for this action to respond w / status 401 rather than redirecting the request to the login page?
I want the console application to be able to respond to this 401 StatusCode, and not transparently. I would also like to keep the default, redirect unauthorized requests to the behavior of the login page.
Note. As a test, I added this to my global.asax and did not bypass the auth form:
protected void Application_AuthenticateRequest(object sender, EventArgs e) { HttpContext.Current.SkipAuthorization = true; }
@ Dale and Andy
I use the AuthorizeAttributeFilter provided in the preview of MVC 4. This returns an HttpUnauthorizedResult. This result sets statusCode correctly to 401. The problem, as I understand it, is that asp.net intercepts the response (starting from 401) and redirects to the login page, rather than just skipping it. I want to get around this interception for specific URLs.
Dane o'connor
source share