首页 文章

AD角色基于MVC3中的授权

提问于
浏览
0

在我们基于MVC3的Intranet应用程序中,一个AD用户可能属于许多角色,在登录期间,他们将选择他们想要登录的角色 . 我们目前正在使用以下方法对用户进行身份验证:http://www.codeproject.com/KB/system/everythingInAD.aspx#35

一旦用户通过身份验证并发现属于他们尝试登录的角色,我们就会根据角色授权访问特定的控制器和操作 . 我们更喜欢使用MVC的Authorize属性 .

由于我们没有使用任何提供程序进行自动化,我们是否还能以某种方式使用Authorize属性来限制访问?

谢谢!巴拉

2 回答

  • 0

    只要您的自定义成员资格提供程序继承自asp.net MemberShipProvider类,您就可以使用Authorize属性 .

    但是,如果您决定使用不从asp.net MembershipProvider类继承的全新提供,则不能使用Authorize attirbute .

  • 0

    @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();
    }
    

    希望这有用 . 祝好运 .

相关问题