如何从 HttpContextBase 获取 ActionExecutingContext
我正在努力实现自定义的 AuthorizeAttribute。 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 };
}
是否可以从传递到 AuthorizeCore 覆盖的 HttpContextBase 获取 ActionExecutingContext?如果没有,有什么建议吗?
感谢您的帮助。
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 ActionExecutingContext from the HttpContextBase passed into AuthorizeCore override? If not, any suggestions?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
AuthorizeCore
< /a> 方法您不需要执行任何重定向。您只需使用 Http Context 返回 true 或 false,具体取决于用户是否经过身份验证和授权。为了将他重定向到错误页面,您需要覆盖HandleUnauthorizedRequest
方法,其中AuthorizationContext
作为参数传递,您可以处理这种情况。当AuthorizeCore
返回false
时,将调用此方法,以便您可以采取相应的操作。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 theHandleUnauthorizedRequest
method where anAuthorizationContext
is passed as argument and you can handle the case. This method will be invoked whenAuthorizeCore
returnsfalse
so that you can act accordingly.