开发者

How to get ActionExecutingContext from HttpContextBase

开发者 https://www.devze.com 2023-02-05 16:29 出处:网络
I\'m working on implementing a customized AuthorizeAttribute.The AuthorizeCore override accepts HttpContextBase.If user does not have the correct role, then I want to throw an error.I found some code

I'm working on implementing a customized AuthorizeAttribute. The AuthorizeCore override accepts HttpContextBase. If user does not have the correct role, then I want to throw an error. I found some code where I can set the MasterName, ViewName, etc to redirect the user to. It uses 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 };
  }

Is it possible to get Ac开发者_C百科tionExecutingContext from the HttpContextBase passed into AuthorizeCore override? If not, any suggestions?

Thanks for your help.


In the AuthorizeCore method you don't need to perform any redirects. You simply need to use the Http Context to return true or false depending on whether the user is authenticated and authorized. In order to redirect him to an error page you need to override the HandleUnauthorizedRequest method where an AuthorizationContext is passed as argument and you can handle the case. This method will be invoked when AuthorizeCore returns false so that you can act accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消