C#学习教程:从UserPrincipal对象获取nETBIOSName在对GroupPrincipal对象调用GetMembers()并过滤结果后,我现在有了UserPrincipal对象的集合GroupPrincipalmyGroup;//这里省略了这个对象的数量foreach(UserPrincipaluserinmyGroup.GetMembers(false).OfType()){Console.WriteLine(user.SamAccountName);上面的代码示例将打印出像“TestUser1”这样的用户名。我需要将这些与格式为“DOMAINTestUser1”的另一个应用程序的列表进行比较。如何从UserPrincipal对象获取“DOMAIN”部分?我不能只附加一个已知的域名,因为涉及多个域,我需要区分DOMAIN1TestUser1和DOMAIN2TestUser2。你有两个我能想到的选择。解析或获取所有内容,即name@fully.qualified.domain.name;使用System.DirectoryServices命名空间。我不知道UserPrincipal,我也不知道GroupPrincipal。另一方面,我知道一种实现你想要的东西的工作方法。[TestCase("LDAP://fully.qualified.domain.name","TestUser1")]publicvoidGetNetBiosName(stringldapUrl,stringlogin)stringnetBiosName=null;字符串foundLogin=null;使用(DirectoryEntryroot=newDirectoryEntry(ldapUrl))使用(DirectorySearchersearcher=newDirectorySearcher(root){searcher.SearchScope=SearchScope.Subtree;searcher.PropertiesToLoad.Add("sAMAccountName");searcher.Filter=string.Format("(&(objectClass=user)(sAMAccountName={0}))",login);SearchResultresult=null;try{result=searcher.FindOne();if(result==null)if(string.Equals(login,结果.GetDirectoryEntry().Properties("sAMAccountName").Value))foundLogin=result.GetDirectoryEntry().Properties("sAMAccountName").Value}finally{searcher.Dispose();root.Dispose();if(result!=null)result=null;}}if(!string.IsNullOrEmpty(foundLogin))使用(DirectoryEntryroot=newDirectoryEntry(ldapUrl.Insert(7,"CN=Partitions,CN=Configuration,DC=").Replace(".",",DC="))使用DirectorySearchersearcher=newDirectorySearcher(root)searcher.Filter="nETBIOSName=*";searcher.PropertiesToLoad.Add("cn");SearchResultCollection结果=null;尝试{结果=searcher.FindAll();if(results!=null&&results.Count>0&&results[0]!=null){ResultPropertyValueCollectionvalues=results[0].Properties("cn");netBiosName=rpvc[0].ToString();}最后{searcher.Dispose();根.处置();if(results!=null){results.Dispose();结果=空;}}}Assert.AreEqual("INTRATESTUSER1",string.Concat(netBiosName,"",foundLogin).ToUpperInvariant())}此SO问题中提供的其他相关信息或链接C#ActiveDirectory:Getuser'sdomainname?如何找到域的NetBIOS名称使用ActiveDsCOM库,它具有内置的名称转换并且不做任何假设(就像这里的其他答案一样)。使用System.Collections.Generic;使用System.Linq;使用系统文本;使用ActiveD;;nameTranslate.Set((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME,userPrincipleName);返回nameTranslate.Get((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4);}}}您是否正在尝试将完全限定的域名传递给另一个应用程序?如果您使用fully_qualified_domainUSER大多数WindowsAPI不会抱怨。您可以在user.DistinguishedName属性中查找可能的域。域1中的用户应包含字符串“DC=DOMAIN1”。它绝对不应该包含字符串“DC=DOMAIN2”。正如对该问题的评论之一所述,我认为这是不久的将来的一个很好的答案:以上是C#学习教程:从UserPrincipal对象获取nETBIOSName如果对你们有用并且需要了解更多,我将分享所有内容关于C#学习教程,希望大家多多关注~user.Sid.Translate(typeof(System.Security.Principal.NTAccount)).ToString()本文收集自网络,不代表立场.如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
