Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorize current user against controller and action name in ASP.NET MVC 3

I need to create a customized authorization in ASP.NET MVC 3. Inside the app, authorization is defined in 5 tables: users, groups, usergroups, rights, grouprights. A user can belong to several groups, and each right can be assigned to several groups too. Each controller action is assigned a RightID.

The built in authorization can't accomodate this setup, so I tried to create a customized AuthorizeAttribute. When overriding AuthorizeCore, I realized I don't have access to controller name and action name.

Can I somehow ask the router to parse the Request.RawUrl inside AuthorizeCore to get controller and action name? Or is there another way to do what I want?

like image 402
Endy Tjahjono Avatar asked Apr 08 '11 11:04

Endy Tjahjono


1 Answers

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
    var routeData = httpContext.Request.RequestContext.RouteData;
    var controller = routeData.GetRequiredString("controller");
    var action = routeData.GetRequiredString("action");
    ...
}
like image 79
Darin Dimitrov Avatar answered Nov 06 '22 11:11

Darin Dimitrov