I'm busy writing my own custom attribute for my action method called MyAuthorizeAttribute, I'm still busy writing code, here is my incomplete code:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class MyAuthorizeAttribute : AuthorizeAttribute { public new Role Roles; public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); if (Roles != 0)
Here is my role enumeration:
public enum Role { Administrator = 1, SuperAdministrator = 2 }
My action method:
[MyAuthorize(Roles = Role.Administrator|Role.SuperAdministrator)] public ActionResult Create() { return View(); }
The reason I did not use Roles = "Administrator, SuperAdministrator" is because the roles are hard-coded. I do not want to have 100 places to change if the role name changes.
Given my method, when it comes to if (Roles! = 0), the total Roles value is 3, how would I check if these 2 roles are included in the list of user roles for a particular user?
Am I doing it right here? If not, how could I implement this? It should not be the way I did it.
source share