Reputation: 65
I have to develop an authorize filter in asp.net mvc.I have got five categories of users in my site and my site uses custom created authentication system.Now i have a controller action which should be accessible to 3 out of those five type of users.How to create a filter (basically authorize) and use it which fulfills my requirement?I think i need to create the authorize filter with parameter.I should be able to use something like this.
Authorize[UsersType="admin,accountant,operator"]
technology used : Asp.net MVC
Thanks in Advance
Upvotes: 2
Views: 3370
Reputation: 18419
UsersType
public class AuthorizeUserAttribute :AuthorizeAttribute
{
private string[] _userType { get; set; }
public AuthorizeUserAttribute(string UsersType)
{
// parse your usertypes here.
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
// do the appropriate assigning and authorizing of methods here
....
base.OnAuthorization(filterContext);
}
}
Now you can put an attribute in your Method in your controller
[AuthorizeUser("admin,accountant,operator")]
public ActionMethod Index()
{
return View();
}
Upvotes: 4