2011-11-16 42 views
0

授权在我们MVC3基于Intranet的应用程序,一个AD用户可以属于多个角色和登录时,他们会选择他们想要登录的角色。我们目前使用以下方法对用户进行身份验证:http://www.codeproject.com/KB/system/everythingInAD.aspx#35AD角色总部设在MVC3

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

因为我们没有使用任何提供商autnenticate,我们仍然可以以某种方式使用的授权属性来限制访问?

谢谢! Bala

回答

0

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

但是,如果你决定要一个全新的提供不从asp.net的MembershipProvider类继承的比你不能使用授权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(); 
} 

希望这个有用的使用。 祝你好运。