我创建了自己的派生 UserPrincipal 类型,用于获取一些扩展的 AD 属性。这工作正常。
现在我正在寻找一种方法来使用 groupprincipal 对象的 GetMembers() 方法来返回我的自定义 UserPrincipal 类型的列表。
FindByIdentityWithType 在 UserPrincipal 上的工作方式有点相同,其中有一个重载,您可以在其上指定自己的 PrincipalType。
有没有办法在 GetMembers 方法上做到这一点?
我创建了自己的派生 UserPrincipal 类型,用于获取一些扩展的 AD 属性。这工作正常。
现在我正在寻找一种方法来使用 groupprincipal 对象的 GetMembers() 方法来返回我的自定义 UserPrincipal 类型的列表。
FindByIdentityWithType 在 UserPrincipal 上的工作方式有点相同,其中有一个重载,您可以在其上指定自己的 PrincipalType。
有没有办法在 GetMembers 方法上做到这一点?
我最终使用 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;
}
}
}
我没有找到一种从 GetMembers 函数返回自定义用户主体的简单方法。我什至无法将返回的 UserPrincipal 转换为我的自定义 userprincipal。
最后,我通过使用 PrincipalSearcher 对象上的 FindAll 方法从我的 OU 中获取所有用户并将其查询过滤器设置为我的自定义 userprincipal 的新类型,从而解决了这个问题。
然后使用基本 userprincipal 类的 GetGroups 方法检查每个用户是否是组的成员。