- 由於創建一個繼承的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();
}