扩展GroupPrincipal和Members属性

我想扩展GroupPrincipal类来处理一些自定义属性:

 using System.DirectoryServices.AccountManagement; [DirectoryRdnPrefix("CN")] [DirectoryObjectClass("group")] public class MyGroupPrincipal : GroupPrincipal { // ... } 

我如何覆盖MyGroupPrincipalMembers属性,以便如果它有一个组成员,则返回MyGroupPrincipal而不是GroupPrincipal的实例? 我想写一下

 MyGroupPrincipal group = GetGroup(); foreach (var m in group.Members) { if (m is MyGroupPrincipal) { // always fails: m is a normal GroupPrincipal // do something } } 

无法直接覆盖GroupPrincipal的Members属性。 相反,你可以滚动你自己的方法(抱歉没有干净的切割代码,但我已经通过我的代码使用了下面描述的部分解决方案)。

我已经多次使用AccountManagement库发现您必须使用基DirectoryEntry来正确完成工作。 您可以使用group.GetUnderlyingObject()访问基础对象,然后通过迭代deGroup.Properties("member")读取成员资格。 读取每个成员类型(不记得属性名称,可能是’member.SchemaClassName’?)和distinguishedName( member.Properties("distinguishedName")(0).ToString() )然后根据你所在的类型创建一个switch语句使用专有名称MyGroupPrincipal.FindByIdentity(context, distinguishedName)创建每个主体,并为用户等执行相同操作…