问题描述
我使用如下所示的自定义AuthorizationFilter:
I use a custom AuthorizationFilter like the followings:
public class ActionAuthorizeAttribute : AuthorizeAttribute {
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) {
if(!httpContext.User.Identity.IsAuthenticated)
return false;
if(IsUserExcluded())
return false;
else
return IsRoleAuthorize(httpContext);
}
}
我在每个操作的顶部都使用此过滤器,并且要检查是否被授权,需要操作名称,控制器名称和区域名称.因此,有什么方法可以像System.Web.HttpContextBase
这样在AuthorizeCore()
方法中获取此名称?如果答案为否",那么我如何获得该名称并将其传递给属性,显然我不想手动添加每个名称,实际上是在控制器中类似于ViewContext.RouteData.Values["Controller"]
的内容:
I use this filter at the top of each action I have, and for check Is Authorized, need Action Name, Controller Name, And Area Name. So is there any way to get this names in AuthorizeCore()
method like use System.Web.HttpContextBase
? if answer is No then how can I get this names and pass it to attribute, obviously I don't want to add each name by hand, actually something likeViewContext.RouteData.Values["Controller"]
in controllers:
[ActionAuthorize(actionName=Action, controller=ControllerName, area=AreaName)]
public ActionResult Index() {
return View();
}
有人对此有任何想法吗?
Does any one have any idea about it?
推荐答案
您可以从RouteData中获取它们:
You could fetch them from the RouteData:
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
var rd = httpContext.Request.RequestContext.RouteData;
string currentAction = rd.GetRequiredString("action");
string currentController = rd.GetRequiredString("controller");
string currentArea = rd.Values["area"] as string;
...
}
这篇关于获取ActionName,ControllerName和AreaName并将其传递给ActionFilter属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!