【问题标题】:AD Roles based Authorization in MVC3MVC3 中基于 AD 角色的授权
【发布时间】:2011-11-16 07:44:35
【问题描述】:
【问题讨论】:
标签:
asp.net-mvc-3
authentication
【解决方案1】:
只要您的自定义成员资格提供程序继承自 asp.net MemberShipProvider 类,您就可以使用 Authorize 属性。
但是,如果您决定拥有一个不从 asp.net MembershipProvider 类继承的全新提供,那么您就不能使用 Authorize attirbute。
【解决方案2】:
@Pankaj 是对的,但您可以为考试定义自定义属性:class MyAuthorizationAttribute : FilterAttribute, IAuthorizationFilter 并覆盖它的 OnAuthorization 方法。然后用这个自定义属性装饰每个动作,并在 OnAuthorization 的正文中计算授权。这是一个示例:
public class MyAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
{
public string _name;
public MyAuthorizationAttribute(string name)
{
this._name = curPerm.name;
}
public void OnAuthorization(AuthorizationContext filterContext)
{
// Calculate permissions...
if (!permit)
{
Exception e = new MethodAccessException("Access to partial denied!");
throw new Exception("Can't access via address bar!", e);
}
}
}
并在行动中使用
[MyAuthorizationAttribute ("Add")]
public ActionResult Index()
{
ViewBag.Message = "About page";
return View();
}
希望这有用。
祝你好运。