Asp.net mvc 2 如何从HttpContextBase获取ActionExecutingContext

Asp.net mvc 2 如何从HttpContextBase获取ActionExecutingContext,asp.net-mvc-2,authorization,Asp.net Mvc 2,Authorization,我正在实现一个定制的属性。AuthorizeCore覆盖接受HttpContextBase。如果用户没有正确的角色,那么我想抛出一个错误。我找到了一些代码,可以在其中设置MasterName、ViewName等以重定向用户。它使用ActionExecutingContext: private void ThrowError(ActionExecutingContext filterContext, string message) { var ex = new Exception(mes

我正在实现一个定制的属性。AuthorizeCore覆盖接受HttpContextBase。如果用户没有正确的角色,那么我想抛出一个错误。我找到了一些代码,可以在其中设置MasterName、ViewName等以重定向用户。它使用ActionExecutingContext:

private void ThrowError(ActionExecutingContext filterContext, string message)
  {
   var ex = new Exception(message);
   var errorInfo = new HandleErrorInfo(ex, filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName);
   var viewData = new ViewDataDictionary(errorInfo);

   filterContext.Result = new ViewResult { MasterName = MasterName, ViewName = ViewName, ViewData = viewData };
  }
是否可以从HttpContextBase获取ActionExecutingContext,并将其传递到AuthorizeCore覆盖中?若否,有何建议?


感谢您的帮助。

在该方法中,您不需要执行任何重定向。您只需要使用Http上下文返回true或false,这取决于用户是否经过身份验证和授权。为了将他重定向到错误页面,您需要重写方法,在该方法中,
AuthorizationContext
作为参数传递,您可以处理该情况。当
AuthorizeCore
返回
false
时,将调用此方法,以便您可以相应地执行操作。

我把它弄得太复杂了。谢谢你的建议。