【问题标题】:If Controller.OnAuthorization() returns void then how do I deny access?如果 Controller.OnAuthorization() 返回 void 那么我如何拒绝访问?
【发布时间】:2011-07-22 20:27:39
【问题描述】:

我本来希望它返回 'true' 或 'false'...

我已经在我的控制器中覆盖了 OnAuthorization 并且基于丢失或无效的 HTTP 标头值我想返回 403 禁止,但是我似乎无法弄清楚如何从 OnAuthorization 返回任何内容,以便它实际上会停止其余的控制器的运行。

我该怎么办?

我在下面的第一次尝试是一个巨大的失败,我认为 Deny() 正在运行但没有任何反应......

public class AuthController : Controller
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (string.IsNullOrEmpty(filterContext.HttpContext.Request.Headers["Authorization"]))
                Deny();

            string authString = filterContext.HttpContext.Request.Headers["Authorization"];

            base.OnAuthorization(filterContext);
        }

        private ActionResult Deny()
        {
            HttpContext.Response.StatusCode = 403;

            return Content("Access Denied", "text/plain");
        }
    }

UPDATE 看起来像这样做了,为什么这可能是一个不好的方法?

    if (string.IsNullOrEmpty(filterContext.HttpContext.Request.Headers["Authorization"]))
    {
        filterContext.Result = Content("Access Denied", "text/plain");
        filterContext.HttpContext.Response.StatusCode = 403;

        base.OnAuthorization(filterContext);
    }

UPDATE AGAIN 好的,所以现在它根本不起作用...我设置了一个断点并观察它进入 if 语句,然后调用 base.OnAuthorization(...) ,然后再次退出......如果它没有执行,为什么它会进入 if 语句?如果它正在执行,为什么调用 base.OnAuthorization(...) 不会提前结束?

【问题讨论】:

  • 您应该发布您的更新作为您自己问题的答案。
  • @BrianDriscoll 它停止工作:(

标签: asp.net-mvc-3 overriding onauthorization


【解决方案1】:

你可以抛出一个 httpexception:

throw new HttpException(403, "Access Denied");

【讨论】:

  • 正是我最终采用的解决方案。
【解决方案2】:

怎么样?

throw new UnauthorizedAccessException();

【讨论】:

    【解决方案3】:

    另一种保护应用程序的方法: Securing your ASP.NET MVC 3 Application

    【讨论】:

      【解决方案4】:

      如何使用这样的路线:

      filterContext.Result.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Message", action = "AccessDenied" }));
      

      :)

      【讨论】:

        【解决方案5】:

        实际上,当为 ActionResult 调用控制器时,页面需要一个 View。 您可以实现以下 UI 以便用户更好地理解:

        //code for checking whether authorized goes here...//
        bool isAuthorised = SomeFunction();
        if (!isAuthorised)
        {
            var viewData = new ViewDataDictionary();
            viewData.Add("Message", "You do not have sufficient privileges for this operation.");
            filterContext.Result = new ViewResult { ViewName = "Unauthorized", ViewData = viewData };
            //The View "Unauthorized" is merely a View page that could inherit from a master View or none,       
            //and could be situated in your Views\Shared folder.
            //And you can present the message in the View page like this or so: 
            //<div style="color:Red; font-size:12pt; font-weight:bold;"> <%:ViewData["Message"] %></div>
        }
        return;
        

        视图名称“未授权”可以是您想要的任何名称,它应该是位于共享视图文件夹中的视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-09
          • 2013-11-14
          • 1970-01-01
          相关资源
          最近更新 更多