3

在我的應用程序中,角色有幾個權限。我希望用戶有權訪問取決於權限的操作,而不是角色。自定義授權(權限)ASP.NET MVC

因此,假設:

  • 管理員擁有PERM1,PERM2,perm3,
  • 超級管理員具有管理員具有+ perm4和perm5所有的任何權限。
  • 此外,還有一些輕微的傢伙也有perm1,perm3,perm6,perm7。

我想要做以下事情:我想讓行動可以通過假設perm3或perm4的人訪問。這兩個權限來自兩個不同的角色。但在perm3 Admin中有perm1和perm2,這個動作也可以由擁有perm3的小個子訪問(它不一定是admin或superadmin)。

那你明白我說的對嗎?我想在ASP.NET MVC 4中實現這一點。因此,我想我需要製作自己的AuthorizeAttribute,我自己的IIdentity並在global.asax中編寫一些方法。 ASP.NET中還有一個成員我是否需要觸摸它?我不知道如何把所有的東西放在一起。誰能幫我嗎?

+0

您可能想要去與asp.net的內置feautre這是基於聲明的身份驗證。 Follow link for more details http://visualstudiomagazine.com/articles/2013/08/01/leveraging-claims-based-security-in-aspnet-45.aspx – hungrycoder

回答

1
public class PermissionAttribute : AuthorizeAttribute 
    { 
     private readonly IAccountService _accountService; 
     private readonly IEnumerable<PermissionEnum> _permissions; 

     public PermissionAttribute(params PermissionEnum[] permissions): 
      this(DependencyResolver.Current.GetService<IAccountService>()) 
     { 
      _permissions = permissions; 
     } 

     protected PermissionAttribute(IAccountService accountService) 
     { 
      _accountService = accountService; 
     } 

     public override void OnAuthorization(AuthorizationContext filterContext) 
     { 
      if(!_permissions.Any(x=>_accountService.HasPermission(filterContext.HttpContext.User.Identity.Name,(int)x))) 
       filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden); 
      base.OnAuthorization(filterContext); 
     } 

    } 
1

基本上你必須創建自己的AuthorizeAttribute,但是使用.NET的IIdentity。您在這裏描述的是基於聲明的身份驗證和授權系統。

很有可能你必須從ASP.NET中扔掉成員資格或僅使用它的一部分。據我所知,它不是建立在索賠的基礎上。

在.NET 4.5中,人們添加了類:ClaimsPrincipal,它實現了IPrincipal接口。這個類可以用來實現基於聲明的定製認證和授權。後來

var id = new ClaimsIdentity(claims, "Dummy"); 
var principal = new ClaimsPrincipal(new[] { id }); 
Thread.CurrentPrincipal = principal; 

,然後只用你在找Thread.CurrentPrincipal中的要求:

因此,當用戶通過驗證後,就可以在線程上添加索賠。

在ASP.NET MVC中,你可以做以下步驟:

  1. 創建委派處理程序,其對用戶進行認證。如果用戶通過身份驗證,則將聲明添加到線程主體。理想情況下,這個委託處理程序應該儘可能高,以便您可以在執行鏈中隨處可用。還記得設置HttpContext.Current.User具有相同的主要

    公共類AuthHandler:DelegatingHandler {

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
    { 
    //authenticate user 
    //get claims 
    //create principal 
    var newPrincipal = CreatePrincipal(claims); 
    Thread.CurrentPrincipal = newPrincipal; 
    if (HttpContext.Current != null) 
        HttpContext.Current.User = newPrincipal; 
    return await base.SendAsync(request, cancellationToken); 
    } 
    

    }

  2. 創建其授權基礎上加入線程權利要求的過濾器主要。在這裏你可以做一些比較當前路線和索賠中找到的信息。

0

因此,我認爲你說的是​​: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 
    } 
} 
+0

你也可以嘗試一下「ClaimsAuthorizationManager」http:///msdn.microsoft.com/en-us/library/system.security.claims.claimsauthorizationmanager.aspx –