Extending GroupPrincipal and Members property

北慕城南 提交于 2019-12-08 02:26:49

问题


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
    }
}

回答1:


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...



来源:https://stackoverflow.com/questions/2668818/extending-groupprincipal-and-members-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!