当前位置: 首页 > 编程语言 > C#

从ActiveDirectory中的一个组中获取所有用户分享

时间:2023-04-10 21:20:31 C#

从ActiveDirectory中的一个组中获取所有用户我正在尝试获取AD中特定组的所有用户并将员工列表返回到我的员工属性在课堂里。我有:我的过滤器没有产生任何结果-它应该是什么?另外,我在这里尝试了第一个解决方案:ListofusersinaspecificActiveDirectorydistributiongroup,但我需要移动设备、扩展等详细信息,我无法使用该方法获取这些信息。publicstaticListCreateEmployeeList(stringdepartment){列出员工=newList();stringfilter=string.Format("(&(ObjectClass=person)(memberOf=CN={0},OU=Users&Groups,OU=Blah,DC=Blah,DC=Blah,DC=Blah))",department);DirectoryEntryadRoot=newDirectoryEntry("LDAP://"+domain,null,null,AuthenticationTypes.Secure);DirectorySearcher搜索器=newDirectorySearcher(adRoot);searcher.SearchScope=SearchScope.Subtree;searcher.ReferralChasing=ReferralChasingOption.All;searcher.Filter=过滤器;SearchResultCollection结果=searcher.FindAll();foreach(SearchResultuserinresults){//对条目做任何你需要做的事情if(user!=null){UserDirectoryEntry=user.GetDirectoryEntry();字符串显示名称=GetUserProperty("显示名称");stringfirstName=GetUserProperty("givenName");stringlastName=GetUserProperty("sn");字符串电子邮件=GetUserProperty("邮件");stringtel=GetUserProperty("电话号码");字符串扩展=GetUserProperty("ipphone");stringmobile=GetUserProperty("手机");字符串标题=GetUserProperty("描述");employees.Add(newEmployee{FullName=displayName,FirstName=firstName,Surname=lastName,Email=email.ToLower(),Telephone=tel,Extension=extension,Mobile=mobile,JobTitle=title});}}返回员工;}using(varcontext=newPrincipalContext(ContextType.Domain,"domainName")){using(vargroup=GroupPrincipal.FindByIdentity(context,"groupName")){if(group==null){MessageBox.Show("组不存在”);}else{varusers=group.GetMembers(true);foreach(UserPrincipaluserinusers){//user变量有关于用户的详细信息}}}}这应该返回组中的所有ActiveDirectory用户使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用System.DirectoryServices;namespaceADQuery{classProgram{staticvoidMain(string[]args){GetListOfAdUsersByGroup("domain","group");控制台.ReadLine();}publicstaticvoidGetListOfAdUsersByGroup(stringdomainName,stringgroupName){DirectoryEntryentry=newDirectoryEntry("LDAP://DC="+domainName+",DC=com");DirectorySearchersearch=newDirectorySearcher(入口);字符串查询="(&(objectCategory=person)(objectClass=user)(memberOf=*))";search.Filter=查询;search.PropertiesToLoad.Add("memberOf");search.PropertiesToLoad.Add("名称");System.DirectoryServices.SearchResultCollectionmySearchResultColl=search.FindAll();Console.WriteLine("{1}域中{0}组的成员",groupName,domainName);foreach(SearchResultresultinmySearchResultColl){foreach(stringpropinresult.Properties["memberOf"]){if(prop.Contains(groupName)){Console.WriteLine(""+result.Properties["name"][0].ToString());}}}}}}祝你好运!以Dalton的示例为基础,这里有简洁的代码来获取组的用户名:(pc,groupName))返回gp==null?null:newSortedSet(gp.GetMembers(true).Select(u=>u.SamAccountName));以下代码将递归搜索嵌套域本地组和/或全局组以查找用户您可以修改此选项以按任何顺序查看组以满足您的需要或返回您想要的任何类型的组。//设置列表返回并获取我们正在查找的组。列表列表=新列表();GroupPrincipalgroup=GroupPrincipal.FindByIdentity(newPrincipalContext(/*此处的连接信息*/),((groupName.Length>0)?groupName:this.Properties.Name));//为组中的每个成员添加所有用户。foreach(Principalprincingroup.Members){/*要更改您要查找的内容或查找方式,只需更改以下某些条件以匹配您想要的内容。*///如果此成员是用户,则添加他们。if(princ.StructuralObjectClass=="user"){list.Add(UserPrincipal.FindByIdentity(newPrincipalContext(/*connectioninfohere*/),princ.Name);}//如果我们递归查找并且这个成员是一个GL_Group然后获取其中的用户并添加它们。(是的,princ.N我));}}返回列表;在这篇文章中,我写了一些适用于ActiveDirectory2003和2008R2的东西,我使用MicrosoftLDAP_MATCHING_RULE_IN_CHAIN。此服务使用DirectoryServices。请小心使用此代码,因为存在双重搜索。但您也可以使用.NETFramework3.5中的管理目录安全主体来执行此操作。你可以阅读这篇文章。您必须获取GroupPrincipal并且您正在寻找Members属性。它还存在于StackOverflow中的其他条目。以上是C#学习教程:获取ActiveDirectory中所有群组的所有用户共享的所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: