在MVC3 Action中使用Response.End。

问题描述 投票:0回答:2

我想做一些类似 本文 建议在我的MVC3站点中使用。 然而,我不确定我可以在我的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
        }
c# asp.net-mvc-3
2个回答
5
投票

这应该可以做到你想要的。

return new HttpUnauthorizedResult();

这将返回一个HTTP 401给浏览器。 请看文档.

© www.soinside.com 2019 - 2024. All rights reserved.