2010-08-12 44 views
2

我必須在asp.net中開發授權過濾器mvc.I在我的網站中有五類用戶,我的站點使用自定義創建的身份驗證系統。現在我有一個控制器行動應該可以訪問這五類用戶中的三類。如何創建一個過濾器(基本上是授權),並使用它滿足我的要求?我想我需要創建帶參數的授權過濾器。我應該能夠使用像這樣的東西。使用參數asp.net mvc創建授權過濾器mvc

授權[UsersType = 「管理,會計,經營者」]

技術應用於:Asp.net MVC

提前

回答

4
  1. 由於創建一個繼承的AuthorizeAttribute類的屬性類MVC。
  2. 在屬性類中創建一個接受參數的構造函數UsersType
  3. 重寫需要的AuthorizeAttribute的相應方法。
  4. 解析您的適當覆蓋方法中的參數。

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