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 i开发者_StackOverflow中文版n 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
}
This should do what you want:
return new HttpUnauthorizedResult();
which will return a HTTP 401 to the browser. See the docs.
精彩评论