Sae*_*eid 35 asp.net-mvc authorization authorize-attribute action-filter asp.net-mvc-3
我使用自定义AuthorizationFilter,如下所示:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在每个操作的顶部使用此过滤器,并且要检查是否已授权,需要操作名称,控制器名称和区域名称.那么有没有办法在AuthorizeCore()
使用方法中获得这些名称System.Web.HttpContextBase
?如果答案是否,那么我怎么能得到这个名字并将其传递给属性,显然我不想手工添加每个名字,实际上就像ViewContext.RouteData.Values["Controller"]
在控制器中一样:
[ActionAuthorize(actionName=Action, controller=ControllerName, area=AreaName)]
public ActionResult Index() {
return View();
}
Run Code Online (Sandbox Code Playgroud)
有人对此有任何想法吗?
Dar*_*rov 83
您可以从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;
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27614 次 |
最近记录: |