因此,我認爲你說的是:ActionA只能訪問,如果用戶有PERM1,PERM2,同樣ActionB是可訪問的,當用戶有PERM1和perm3
我給的代碼是爲了說明,我沒有編譯它。但是會給你方法的畫面我很說明
STEP 1:您可以創建具有標誌歸因權限枚舉進行屬性
STEP 2:加入聲稱目前的主要依據用戶權限存儲在數據存儲中。
步驟3:當操作被調用針對權利要求
[Flags]
enum PermType
{
None = 0x0,
Perm1 = 0x1,
perm2 = 0x2,
perm3 = 0x4,
perm4 = 0x8,
perm5 = 0x10
}
添加的權利要求書中所述CurrentPrincipal
var currentPrincipal = ClaimsPrincipal.Current;
var cms = currentPrincipal.Claims;
var permissions = PermType.Perm1 | PermType.perm2;
var claims = cms.ToList();
claims.Add(new Claim("Action1", permissions.ToString()));
claims.Add(new Claim("Action2", permissions.ToString()));
claims.Add(new Claim("Action3", permissions.ToString()));
System.Threading.Thread.CurrentPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims));
檢查授權訪問如果用戶可以訪問的特定動作
public bool CanAccessThisAction(string acionName,PermType requiredPerms)
{
var claim = principal.Claims.FirstOrDefault(c => c.Type == acionName);
if (customPermissionClaim != null)
{
//check if required permission is present in claims for this user
//return true/false
}
return false;
}
行動
public ActionResult TestAction(string id)
{
if(CanAccessThisAction("TestAction",PermType.Perm1|PermType.perm3|PermType.perm5))
{
//do your work here
}
else
{
//redirect user to some other page which says user is not authorized
}
}
您可能想要去與asp.net的內置feautre這是基於聲明的身份驗證。 Follow link for more details http://visualstudiomagazine.com/articles/2013/08/01/leveraging-claims-based-security-in-aspnet-45.aspx – hungrycoder