【问题标题】:Getting members of an AD group where result type is a custom derived UserPrincipal获取结果类型为自定义派生 UserPrincipal 的 AD 组的成员
【发布时间】:2012-04-06 11:13:04
【问题描述】:

我创建了自己的派生 UserPrincipal 类型,用于获取一些扩展的 AD 属性。这很好用。

现在我正在寻找一种方法来使用 groupprincipal 对象的 GetMembers() 方法来返回我的自定义 UserPrincipal 类型的列表。

FindByIdentityWithType 在 UserPrincipal 上的工作方式有点相同,其中有一个重载,您可以在其上指定自己的 PrincipalType。

有没有办法在 GetMembers 方法上做到这一点?

【问题讨论】:

    标签: .net active-directory directoryservices userprincipal


    【解决方案1】:

    我最终使用 AdvancedSearchFilter 来构造 LDAP memberof 查询,而不是 GroupPrincipal.GetMembers。

        static void Main(string[] args)
        {
            using (var context = new PrincipalContext(ContextType.Domain))
            {
                var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, "Group Name");
    
                UserPrincipalEx qbe = new UserPrincipalEx(context);
                qbe.AdvancedSearchFilter.MemberOf(group);
                PrincipalSearcher searcher = new PrincipalSearcher(qbe);
    
                var all = searcher.FindAll().OfType<UserPrincipalEx>().ToList();
    
                foreach (var member in all)
                {
                    Console.WriteLine(member.DisplayName);
                }
            }
        }
    
        public class UserPrincipalExSearchFilter : AdvancedFilters
        {
            public UserPrincipalExSearchFilter(Principal p) : base(p) { }
    
            public void MemberOf(GroupPrincipal group)
            {
                this.AdvancedFilterSet("memberof", group.DistinguishedName, typeof(string), MatchType.Equals);
            }
        }
    
        [DirectoryRdnPrefix("CN")]
        [DirectoryObjectClass("User")]
        public class UserPrincipalEx : UserPrincipal
        {
            private UserPrincipalExSearchFilter searchFilter;
    
            public UserPrincipalEx(PrincipalContext context)
                : base(context)
            {
    
            }
    
            public UserPrincipalEx(PrincipalContext context,
                                 string samAccountName,
                                 string password,
                                 bool enabled)
                : base(context,
                       samAccountName,
                       password,
                       enabled)
            {
            }
    
            public new UserPrincipalExSearchFilter AdvancedSearchFilter
            {
                get
                {
                    if (null == searchFilter)
                        searchFilter = new UserPrincipalExSearchFilter(this);
    
                    return searchFilter;
                }
            }
        }
    

    【讨论】:

    • 对此的一个(更正)评论 - 您可能很想使用 qbe.IsMemberOf() 而不是额外的搜索过滤器,但我的经验是这不起作用。所以不要这样做。
    【解决方案2】:

    我没有找到一种从 GetMembers 函数返回自定义用户主体的简单方法。我什至无法将返回的 UserPrincipal 转换为我的自定义 userprincipal。

    最后,我通过使用 PrincipalSearcher 对象上的 FindAll 方法从我的 OU 中获取所有用户并将其查询过滤器设置为我的自定义 userprincipal 的新类型,从而解决了这个问题。

    然后使用基本 userprincipal 类的 GetGroups 方法检查每个用户是否是组的成员。

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多