Jeff Reddy
Jeff Reddy

Reputation: 5670

Use of Response.End in an MVC3 Action

I'm trying to do something like this article suggests in my MVC3 site. However, I'm not sure I can use the Response.End in my Action.

My question is, how can I return a 401 status code from my Action if the HttpContext.User == null?

public ActionResult WinUserLogOn(string returnUrl) {
        var userName = string.Empty;

        if (HttpContext.User == null) {
            //This will force the client's browser to provide credentials
            Response.StatusCode = 401;
            Response.StatusDescription = "Unauthorized";
            Response.End();
            return View("LogOn"); //<== ????
        }
        else {
           //Attempt to Log this user against Forms Authentication
        }

Upvotes: 0

Views: 2854

Answers (1)

m.edmondson
m.edmondson

Reputation: 30862

This should do what you want:

return new HttpUnauthorizedResult();

which will return a HTTP 401 to the browser. See the docs.

Upvotes: 5

Related Questions