I have an MVC application which authenticates against Active Directory using Forms authentication. As my MVC application has gown in size, it has become apparent that the role checking I am doing is becomming more and more fragmented. I want to replace things like:
[Authorize(Roles = "Staff")] and Thread.CurrentPrincipal.IsInRole("Staff")
with something along the lines of:
[AuthorizePermission(Permission.CanDoSomething)] and Thead.CurrentPrincipal.HasPermission(Permission.CanDoSomething)
where Permission is an enum. Now I was thinking that I could define which permissions each AD role has in the web.config like so:
<role name="Staff">
<permissions>
<add name="CreateEditDeleteSomething" />
<add name="PublishSomething" />
<add name="QueryUsers" />
</permissions>
</role>
I could then implement an IPrincipal extension method – HasPermission(Permission permission). This would see if the User belogs to any of the AD groups which have the the passed in permission as defined in the web.config. This would allow me to alter the permissions a particular AD goup has without having to change the code or update the existing tests. The custom Authorize Attribute could then call the HasPermission method.
Is this approach correct or are there better ways of simplifying my roles within the application? I have seen a number of examples on here and around the web but they seem to be overly complicated. Can I acheive this by just checking the passed in Permission against my web.config role settings in HasPermission? The IPrincipal will have its AD roles so surely it is straightforward to then determine the permissions allowed?
Any help appreciated!
Permissions are much more complicated than roles.
There can often be permission groups such as CreateEditDelete… but their can also be granular subsets such as just “Create”, “Edit”, “Delete”
The way I would solve this problem is to create a PermissionsManger class that can determine which permissions that a user should have given the business rule context and their AD roles.
I used bitwise flags to help simplify the complications of granular permissions.
How you map the roles to permissions is completely up to you.