在MVC3操作中使用Response.End

时间:2011-09-23 15:49:41

标签: c# asp.net-mvc-3

我正试图在我的MVC3网站上做this article建议的事情。但是,我不确定我可以在我的Action中使用Response.End。

我的问题是,如果HttpContext.User == null,如何从我的Action中返回401状态代码?

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
        }

1 个答案:

答案 0 :(得分:5)

这应该做你想要的:

return new HttpUnauthorizedResult();

将HTTP 401返回给浏览器。 See the docs