2011-11-16 41 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(); 
} 

希望這個有用的使用。 祝你好運。