I want to write my own attribute, which will be similar to [Authorize(Role = "Admin", Permission = "Create")] so that I can use this construct in front of the class. Permission for admin is stored in a separate database table.
UPD
Intended use:
public ExampleController : Controller{ ... [Authorize(Role = "Admin", Permisson = "Edit")] public ActionResult Example(){ } } What is there?
There was an idea to inherit from AuthorizeAttribute and add the Permissions property
public class ExampleAuthorizeAttribute : AuthorizeAttribute { private string[] allowedUsers; private string[] allowedRoles; private string[] allowedPermission; public string Permissions { set { allowedPermission = value.Split(',', ' ').ToArray(); } } public ExampleAuthorizeAttribute(string[] users, string[] roles) { allowedUsers = users; allowedRoles = roles; } // здесь перед return проверить Permissions protected override bool AuthorizeCore(HttpContextBase httpContext) { return httpContext.Request.IsAuthenticated && hasUser(httpContext) && hasRole(httpContext); } private bool hasUser(HttpContextBase httpContext) { return allowedUsers.Length > 0 ? allowedUsers.Contains(httpContext.User.Identity.Name) : true; } private bool hasRole(HttpContextBase httpContext) { return allowedRoles.Length > 0 ? allowedRoles.Any(role => httpContext.User.IsInRole(role)) : true; } } And then use it, but it’s completely understandable how to put the Permissions of the user into context
public ExampleController : Controller{ ... [ExampleAuthorize(Role = "Admin", Permisson = "Edit")] public ActionResult Example(){ } } But it seems that not quite the right decision
ps mvc 4