简体   繁体   中英

MVC 3 Action / Controller Attributes / Permissions

To keep this example simple, my site has two controllers, UsersController & VenuesController, both of which have their own associated "admin permission".

So a user can have the "Users" admin permission and/or the "Venues" admin permission (these are bool fields on the users table in my db)

The user logs in and I have his/her permissions in session.

I want to decorate my controllers with a custom attribute to check the user has the correct permission to use the controller. I don't want to create a UserPermissionAttribute and a VenuePermissionAttribute, ideally I want a single attribute, that takes a bool property in and checks that it's true, so something like;

[CheckPermissionAttribute(_userSession.AdminPermissions.Users)]
public class UsersController : Controller
{

}

and

[CheckPermissionAttribute(_userSession.AdminPermissions.Venues)]
public class VenuesController : Controller
{

}

Is this possible to achieve, or should I be looking at PostSharp or something else to achieve the same results?

you can create attribute with a parameter. Action filter parameters are properties of the action filter.

but it's better to create users roles and user the standart approach with AuthorizeAttribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM