使用参数asp.net mvc创建授权过滤器mvc
问题描述:
我必须在asp.net中开发授权过滤器mvc.I在我的网站中有五类用户,我的站点使用自定义创建的身份验证系统。现在我有一个控制器行动应该可以访问这五类用户中的三类。如何创建一个过滤器(基本上是授权),并使用它满足我的要求?我想我需要创建带参数的授权过滤器。我应该能够使用像这样的东西。使用参数asp.net mvc创建授权过滤器mvc
授权[UsersType = “管理,会计,经营者”]
技术应用于:Asp.net MVC
提前
答
- 由于创建一个继承的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();
}