0

我想知道在用戶登錄到應用程序時創建CustomRoles。 我有特徵模型,看起來像如下:身份框架的自定義角色提供程序

public class Feature : AuditableEntity 
    { 
     [Display(Name = "Code")] 
     [MaxLength(50)] 
     public string FeatureCode { get; set; } 
    } 

我想根據特徵模型的FeatureCode創建角色,這樣當用戶登錄應用程序分配給該perticular用戶的角色行爲。

我想用這樣的:

布爾值= user.isInRole(FeatureCode)

,將返回真或假根據所分配的功能,給用戶。 在此先感謝。

回答

0

我已經使用ClaimsAuthentionManager類提供了一種機制來轉換具有聲明(角色)的傳入用戶。這裏是一個自定義ClaimsAuthenticationManager的一些示例代碼:

public class ClaimsTransformationModule : ClaimsAuthenticationManager 
{ 
    public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal) 
    { 
     if (incomingPrincipal != null && incomingPrincipal.Identity.IsAuthenticated == true) 
     { 
      Claim nameIdentifier = incomingPrincipal.Claims.Where(foo => foo.Type == ClaimTypes.Name).FirstOrDefault(); 
      var roles = GetRoles(nameIdentifier.Value); // Get the roles from the backend based on the user 
      foreach (var role in roles) //This is the part applying roles to the Claim (user) 
      { 
       ((ClaimsIdentity)incomingPrincipal.Identity).AddClaim(new Claim(ClaimTypes.Role, role)); 
      } 
      ((ClaimsIdentity)incomingPrincipal.Identity).AddClaim(new Claim(ClaimTypes.Sid, GetUserId(nameIdentifier.Value))); 
     } 

     return incomingPrincipal; 
    } 

然後在Web.config你可以將系統配置爲使用自定義索賠經理:

<system.identityModel> 
<identityConfiguration> 
    <claimsAuthenticationManager type="ClaimsTransformation.ClaimsTransformationModule, ClaimsTransformation" /> 

然後拿到角色的當前登錄的用戶你去:

var user = ClaimsPrincipal.Current; 
bool isInRole = user.IsInRole(roleName); 

但看看leastprivilege.com網站了解更多信息。

乾杯弗蘭克

相關問題