I want to protect the action of the controller so that only users with the Administrator role can enter it.
I do not use a role / membership provider, all this is custom.
I have done it so far:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.AuthorizeCore(httpContext); if (!isAuthorized) return false; string username = httpContext.User.Identity.Name; UserRepository repo = new UserRepository(); return repo.IsUserInRole(username, "Admin"); } }
Please note that I hardcoded "admin" here.
I want it to be dynamic.
This work now:
[CustomAuthorize] public ActionResult RestrictedArea()...
But I want something like this:
[CustomAuthorize(Roles = "Admin")] public ActionResult RestrictedArea()
c # asp.net-mvc asp.net-mvc-3 custom-attributes
1110
source share