- 由于创建一个继承的AuthorizeAttribute类的属性类MVC。
- 在属性类中创建一个接受参数的构造函数
UsersType
- 重写需要的AuthorizeAttribute的相应方法。
- 解析您的适当覆盖方法中的参数。
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);
}
}
现在你可以把一个属性在你的方法在你的控制器
[AuthorizeUser("admin,accountant,operator")]
public ActionMethod Index()
{
return View();
}