扩展GroupPrincipal和成员财产 [英] Extending GroupPrincipal and Members property
问题描述
我想延长 GroupPrincipal
类来处理一些自定义属性:
使用System.DirectoryServices.AccountManagement;
[DirectoryRdn preFIX(CN)]
[DirectoryObjectClass(本集团)]
公共类MyGroupPrincipal:GroupPrincipal {
// ...
}
我怎么能覆盖成员
属性 MyGroupPrincipal
因此,如果它有一个构件是一组的 MyGroupPrincipal
的实例没有和 GroupPrincipal
返回?我想写例如。
MyGroupPrincipal组= GetGroup();
的foreach(在group.Members VAR米){
如果(m为MyGroupPrincipal){//总是失败:m是一个正常的GroupPrincipal
// 做一点事
}
}
有没有办法直接覆盖GroupPrincipal的成员属性。相反,你可以滚你自己的方法(对不起,没有干净的切割code,但我用描述solutiont通过了我的code的下面部分)。
我发现,多次与AccountManagement库,你只需要使用基本的DirectoryEntry把事情做对。您可以使用 group.GetUnderlyingObject()
访问基础对象,然后通过重复读取成员 deGroup.Properties(会员)
。阅读每一个成员的类型(不记得属性名,也许member.SchemaClassName'?)和的distinguishedName( member.Properties(的distinguishedName)(0)的ToString()
)然后创建一个在其中创建使用的专有名称 MyGroupPrincipal.FindByIdentity(背景下,distinguishedName来)
各主要基于类型switch语句,而对于用户来说,等做同样的......
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
}
}
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 iterating deGroup.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 name MyGroupPrincipal.FindByIdentity(context, distinguishedName)
, and do the same for users, etc...
这篇关于扩展GroupPrincipal和成员财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!