从UserPrincipal对象获取nETBIOSName

我正在使用.Net库的System.DirectoryServices.AccountManagement部分连接到ActiveDirectory。

在GroupPrincipal对象上调用GetMembers()并过滤结果后,我现在拥有了UserPrincipal对象的集合

GroupPrincipal myGroup; // population of this object omitted here foreach (UserPrincipal user in myGroup.GetMembers(false).OfType()) { Console.WriteLine(user.SamAccountName); } 

上面的代码示例将打印出“TestUser1”之类的用户名。 我需要将这些与来自“DOMAIN \ TestUser1”格式的另一个应用程序的列表进行比较。

如何从UserPrincipal对象获取“DOMAIN”部分?

我不能只是附加一个已知的域名,因为涉及多个域,我需要区分DOMAIN1 \ TestUser1和DOMAIN2 \ TestUser2。

你有两个我能想到的选择。

  1. 解析或采取所有内容,即name@fully.qualified.domain.name ;
  2. 使用System.DirectoryServices命名空间。

我不知道UserPrincipal ,我也不知道GroupPrincipal 。 另一方面,我知道一种工作方式来实现你想要的东西。

 [TestCase("LDAP://fully.qualified.domain.name", "TestUser1")] public void GetNetBiosName(string ldapUrl, string login) string netBiosName = null; string foundLogin = null; using (DirectoryEntry root = new DirectoryEntry(ldapUrl)) Using (DirectorySearcher searcher = new DirectorySearcher(root) { searcher.SearchScope = SearchScope.Subtree; searcher.PropertiesToLoad.Add("sAMAccountName"); searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", login); SearchResult result = null; try { result = searcher.FindOne(); if (result == null) if (string.Equals(login, result.GetDirectoryEntry().Properties("sAMAccountName").Value)) foundLogin = result.GetDirectoryEntry().Properties("sAMAccountName").Value } finally { searcher.Dispose(); root.Dispose(); if (result != null) result = null; } } if (!string.IsNullOrEmpty(foundLogin)) using (DirectoryEntry root = new DirectoryEntry(ldapUrl.Insert(7, "CN=Partitions,CN=Configuration,DC=").Replace(".", ",DC=")) Using DirectorySearcher searcher = new DirectorySearcher(root) searcher.Filter = "nETBIOSName=*"; searcher.PropertiesToLoad.Add("cn"); SearchResultCollection results = null; try { results = searcher.FindAll(); if (results != null && results.Count > 0 && results[0] != null) { ResultPropertyValueCollection values = results[0].Properties("cn"); netBiosName = rpvc[0].ToString(); } finally { searcher.Dispose(); root.Dispose(); if (results != null) { results.Dispose(); results = null; } } } Assert.AreEqual("INTRA\TESTUSER1", string.Concat(netBiosName, "\", foundLogin).ToUpperInvariant()) } 

本SO问题中提供的其他相关信息或链接。
C#Active Directory:获取用户的域名?
如何查找域的NetBIOS名称

使用ActiveDs COM库,它具有内置名称转换function,并且不做任何假设(如此处的其他答案)。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ActiveDs; namespace Foo.Repository.AdUserProfile { public class ADUserProfileValueTranslate { public static string ConvertUserPrincipalNameToNetBiosName(string userPrincipleName) { NameTranslate nameTranslate = new NameTranslate(); nameTranslate.Set((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME, userPrincipleName); return nameTranslate.Get((int) ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4); } } } 

您是否尝试将完全限定的域名传递给其他应用? 如果你使用fully_qualified_domain\USER大多数windows API都不会抱怨。

您可以在user.DistinguishedName属性中查找可能的域。 域1中的用户应包含字符串“DC = DOMAIN1”。 它绝对不应该包含字符串“DC = DOMAIN2”。

正如对该问题的评论之一所述,我认为这是近期的一个很好的答案:

  user.Sid.Translate(typeof(System.Security.Principal.NTAccount)).ToString()