2011-02-25 37 views
4

我忙着寫我自己的自定義屬性我叫MyAuthorizeAttribute操作方法,我還在忙着寫代碼,這裏是我的部分代碼:如何延長AuthorizeAttribute並檢查用戶的角色

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 
public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    public new Role Roles; 

    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     base.OnAuthorization(filterContext); 

     if (Roles != 0) // Did it this way to see what the value of Roles was 
     return; 

     // Here I am going to get a list of user roles 
     // I'm doing my own database calls 

     filterContext.Result = new HttpUnauthorizedResult(); 
    } 
} 

這裏我的角色枚舉:

public enum Role 
{ 
    Administrator = 1, 
    SuperAdministrator = 2 
} 

我的操作方法:

原因爲什麼我沒有使用角色=「管理員,超級管理員」是因爲角色是硬編碼的。如果角色名稱發生變化,我不希望有100個地方可以更改。

鑑於我的方法,當它到達if(Roles!= 0)時,角色總值爲3,我將如何檢查這兩個角色是否在特定用戶的用戶角色列表中?

我在這裏正確嗎?如果不是,我會如何執行此操作?它不一定是我做它的方式。

+0

是由以下任何兩個答案來解決問題了嗎?如果是,請接受答案。它會幫助像我一樣有同樣問題的其他人。 – 2013-11-21 12:49:35

回答

2

如果我理解正確,這裏的問題不是繼承AuthorizeAttribute,而是通過比較枚舉值。您可能需要一個枚舉類型,您可以使用它作爲位標誌 - 如果是,請查看the section about Enumeration Types in the C# Programming guide,尤其是第二部分「枚舉類型作爲位標誌」。

要澄清一點:

而不只是檢查Roles!=0的,你現在就可以做這樣的事情:

public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    base.OnAuthorization(filterContext); 

    // Here you get an enum indicating the roles this user is in. The method 
    // converts the db information to a Role enum before it is returned. 
    // If the user is not authenticated, the flag should not be set, i.e. equal 0. 
    Role userRole = GetUserRolesFromDatabase(); 

    // Bitwise comparison of the two role collections. 
    if (Roles & userRole > 0) 
    { 
     // The user is in at least one of the roles in Roles. Return normally. 
     return; 
    } 

    // If we haven't returned yet, the user doesn't have the required privileges. 
    new HttpUnauthorizedResult(); 
} 

爲了使對比更容易,你可以使用下面的擴展方法上的枚舉:

public static class RolesExtensions 
{ 
    public static bool HasAnyOf(this Roles r1, Roles roles) 
    { 
     return (r1 & roles) > 0; 
    } 
} 
+0

是我分配的角色,我想檢查用戶是否在角色中。它不一定是這樣,它只是我,可能有更好的方法來實現這一點。 – 2011-02-25 13:42:25

+0

我不明白這將如何幫助我? – 2011-02-25 13:45:42

+0

@Brendan:請參閱我的更新。 – 2011-02-25 14:28:07

4

這豈不是更好,如果MyAuthorizeAttribute接受一個I​​List(或類似) 這樣,它是兩個類型安全,但你不必使用位標誌。 如果你想保存reult,位標誌很棒,但這是另一種方式。

編輯(現在用的例子):

Attribute: 

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 
public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    public Role[] RoleList { get; set; } 


    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (httpContext == null) 
     { 
      throw new ArgumentNullException("httpContext"); 
     } 
     IPrincipal user = httpContext.User; 
     if (!user.Identity.IsAuthenticated) 
     { 
      return false; 
     } 
     //Only role access is implemented here 
     /*if ((this._usersSplit.Length > 0) && !this._usersSplit.Contains<string>(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) 
     { 
      return false; 
     }*/ 
     if ((RoleList.Length > 0) && !RoleList.Select(p=>p.ToString()).Any<string>(new Func<string, bool>(user.IsInRole))) 
     { 
      return false; 
     } 
     return true; 
    } 

} 

控制器:

[MyAuthorize(RoleList = new []{Role.Administrator , Role.SuperAdministrator})] 
    public ActionResult Create() 
    { 
     return View(); 
    } 
+0

您介意提供代表您的意思的代碼示例嗎? – 2011-02-25 14:08:36

+0

@Brendan:添加了示例。 – Tomas 2011-02-25 14:40:18

相關問題