2009-09-20 55 views
1

即時尋找一種方法來定製我的授權屬性,以便我可以用我自己的MembershipProvider正確實現它。自定義授權屬性附加參數?

我需要的是例如IsInRoles(string role,int perm),換句話說,我想用新的IsinRoles取代它,或者可能創建另一種方法來存檔此結果。

可能嗎?或者我需要寫一個不同的授權屬性?

非常感謝您對我們的關注......

PS:IM在ASP.net MVC的工作,所以我需要有[Authorize]過濾器啓動和運行。

回答

5

我認爲你可以添加一個公共屬性到你的自定義AuthorizeAttribute。

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    /// <summary> 
    /// Add the allowed roles to this property. 
    /// </summary> 
    public YourCustomRoles RequiredRole; 

    public int YourCustomValue; 

    /// <summary> 
    /// Checks to see if the user is authenticated and has the 
    /// correct role to access a particular view. 
    /// </summary>   
    /// <param name="httpContext"></param> 
    /// <returns></returns> 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (httpContext == null) throw new ArgumentNullException("httpContext"); 

     // Make sure the user is authenticated. 
     if (httpContext.User.Identity.IsAuthenticated == false) return false; 

     // Can use your properties if needed and do your checks 
     bool authorized = DoSomeCustomChecksHere(); 

     return authorized; 
    } 
} 

用法我想會(還沒有嘗試過,雖然):

[CustomAuthorizeAttribute (RequiredRole=MyCustomRole.Role1 | MyCustomRole.Role2, YourCustomValue=1234)] 
+0

是可行的,例如[Authorize.IsInRole( 「XXX」)控制器之前,過濾器? thnx – DucDigital 2009-09-20 04:22:36

+0

感謝您的這一點,只需將其實施到我的新授權類,並在控制器過濾器中調用它。 :) – DucDigital 2009-09-20 19:20:43