代码之家  ›  专栏  ›  技术社区  ›  Khalil Radhi

C LDAP AD身份验证,使用职务进行授权

  •  0
  • Khalil Radhi  · 技术社区  · 8 年前

    我看过很多关于如何使用基于角色的授权的在线文章和示例代码,但是找不到任何与基于标题的授权相关的内容。

    我在一个类中有以下内容用于AD身份验证:

     protected ClaimsIdentity CreateIdentity(UserPrincipalExtended userPrincipal)
        {
            var identity = new ClaimsIdentity(MyAuthentication.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType,
                ClaimsIdentity.DefaultRoleClaimType);
            identity.AddClaim(new Claim(
                "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
                "Active Directory"));
            identity.AddClaim(new Claim(ClaimTypes.Name, userPrincipal.Name));
            identity.AddClaim(new Claim(ClaimTypes.GivenName, userPrincipal.GivenName ?? string.Empty));
            identity.AddClaim(new Claim(ClaimTypes.Surname, userPrincipal.Surname ?? string.Empty));
            identity.AddClaim(new Claim(userPrincipal.Title, userPrincipal.Title ?? string.Empty));
            identity.AddClaim(new Claim(userPrincipal.Department, userPrincipal.Department ?? string.Empty));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userPrincipal.SamAccountName));
            if (!string.IsNullOrEmpty(userPrincipal.EmailAddress))
                identity.AddClaim(new Claim(ClaimTypes.Email, userPrincipal.EmailAddress));
    
            var claims = new List<Claim>();
            var dirEntry = (DirectoryEntry) userPrincipal.GetUnderlyingObject();
            foreach (string groupDN in dirEntry.Properties["memberOf"])
            {
                var parts = groupDN.Replace("CN=", "").Split(',');
                claims.Add(new Claim(ClaimTypes.Role, parts[0]));
            }
    
    
            if (claims.Count > 0)
                identity.AddClaims(claims);
    
    
            var title = dirEntry.Properties["title"].Value.ToString();
            return identity;
        }
    

    这很好,我可以看到从Active Directory中检索到的正确标题和部门。显然,我使用的是扩展用户主体,因为标题和部门不是标准用户主体的一部分。

    我的问题是我无法将标题和部门值传递给我的控制器进行授权。

    在homecontroller中,我使用以下方法获取经过身份验证的用户的信息:

    var firstname = System.Web.HttpContext.Current.User.GetFirstname();
    var lastname = System.Web.HttpContext.Current.User.GetLastname();
    var email = System.Web.HttpContext.Current.User.GetEmail();
    var uid = System.Web.HttpContext.Current.User.GetUserID();
    

    显然,这使用了标准的iprincipal,它不包含标题和部门的规定。

    是否有一种简单的方法将标题和部门参数传递给控制器?是否可以使用标题来授权访问而不是角色?

    提前谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   pfx    8 年前

    我有一个类似的挑战,通过使用(在这里是您的例子)标题和部门作为角色来解决它。 这允许使用所有现成的基于角色的授权功能。

    if (userPrinciple.Title != null)
    {
        identity.AddClaim(new Claim(ClaimTypes.Role, userPrincipal.Title));
    }
    
    if (userPrincipal.Department != null)
    {
        identity.AddClaim(new Claim(ClaimTypes.Role, userPrincipal.Department));
    }
    

    如果角色是特定于部门的,则可以将两者结合起来。

    identity.AddClaim(new Claim(ClaimTypes.Role, $"{userPrincipal.Department}.{userPrincipal.Title}));
    
    推荐文章