2013-02-19 84 views
0

我想擴展關鍵字的過濾器的授權屬性,而無需創建自定義授權對象。授權屬性擴展新的關鍵字

我知道構建關鍵字是角色,用戶。我想添加一個新的關鍵字調用級別,並查看用戶訪問級別是否大於特定級別。

這將允許我只爲用戶創建一個角色。

這樣做的用法是

[Authorize(level > 1)] 

請讓我知道如果你需要更多的信息,讓您更好地瞭解我試圖解決這個問題。

我看了,但一直沒能解決這個問題。你能提供一個代碼示例嗎?這是我的。當我使用電平過濾器時出現錯誤。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Security; 
[Auth(Level > 1)] 
public ActionResult Index() 
{ 
} 

public class Auth : AuthorizeAttribute 
{ 
    public int Level { get; set; } 
    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     base.OnAuthorization(filterContext); 
    } 
} 

回答

0

這是不可能的,只是增加新的過濾器,內置AuthorizeAttribute。您將需要創建一個從AuthorizeAttribute繼承的新屬性。

有很多關於創建自定義AuthorizeAttributes的博客文章和問題。編輯: 好吧,好開始。你的問題是,你不能像你想要的那樣去做Level > 1。假設您試圖說當用戶Level大於1時您通過了什麼認證,您可以在屬性中更改您的屬性以表明這一點。

E.g.

public class Auth : AuthorizeAttribute 
{ 
    public int MinLevel { get; set; } 

    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     //This is where you will need to determine if the user is authorized based upon the minimum level. 
     base.OnAuthorization(filterContext); 
    } 
} 
+0

我已經看了,並沒有能夠弄清楚如何讓我的自定義身份驗證工作中的新關鍵字。你能提供樣品嗎?這是我到目前爲止。 – Thuthinh 2013-02-19 15:14:10

+0

@ user2085818我爲您提供了一個示例 – 2013-02-19 22:02:30