扩展 GroupPrincipal 和 Members 属性
我想扩展 GroupPrincipal
类来处理一些自定义属性:
using System.DirectoryServices.AccountManagement;
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("group")]
public class MyGroupPrincipal : GroupPrincipal {
// ...
}
如何重写 MyGroupPrincipal
的 Members
属性,以便它有一个成员这是一个组,返回的是 MyGroupPrincipal
的实例,而不是 GroupPrincipal
的实例?我想写例如
MyGroupPrincipal group = GetGroup();
foreach (var m in group.Members) {
if (m is MyGroupPrincipal) { // always fails: m is a normal GroupPrincipal
// do something
}
}
I want to extend the GroupPrincipal
class to handle some custom properties:
using System.DirectoryServices.AccountManagement;
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("group")]
public class MyGroupPrincipal : GroupPrincipal {
// ...
}
How could I override the Members
property for MyGroupPrincipal
so that if it has a member that is a group an instance of MyGroupPrincipal
and not of GroupPrincipal
is returned? I would like to write e.g.
MyGroupPrincipal group = GetGroup();
foreach (var m in group.Members) {
if (m is MyGroupPrincipal) { // always fails: m is a normal GroupPrincipal
// do something
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法直接覆盖 GroupPrincipal 的 Members 属性。相反,您可以推出自己的方法(抱歉,没有干净的代码,但我在我的代码中使用了下面描述的解决方案的部分内容)。
我多次发现,使用 AccountManagement 库时,您只需使用基本 DirectoryEntry 即可正确完成工作。您可以使用
group.GetUnderlyingObject()
访问基础对象,然后通过迭代deGroup.Properties("member")
读取成员资格。读取每个成员类型(记不住属性名称,也许是'member.SchemaClassName'?)和distinguishedName (member.Properties("distinguishedName")(0).ToString()
) 然后创建一个switch 语句基于您使用专有名称创建每个主体的类型MyGroupPrincipal.FindByIdentity(context, DistinguishedName)
,并对用户执行相同的操作,等等...There is no way to directly override the Members property of GroupPrincipal. Instead you can roll your own method (sorry for no clean cut code, but I've used portions of the below described solutiont through out my code).
I've found that many times with the AccountManagement library that you just have to use the base DirectoryEntry to get things done right. You can access the base object by using
group.GetUnderlyingObject()
, then read the membership by iteratingdeGroup.Properties("member")
. Read each members type (can't remember the property name, maybe 'member.SchemaClassName'?) and distinguishedName (member.Properties("distinguishedName")(0).ToString()
) Then create a switch statement based on type where you create each principal using the distinguished nameMyGroupPrincipal.FindByIdentity(context, distinguishedName)
, and do the same for users, etc...