如何从AD DirectoryEntry获取DOMAIN \ USER?

如何从Active Directory DirectoryEntry(SchemaClassName =“user”)对象获取Windows用户和域?

用户名在sAMAccountName属性中,但在哪里可以查找域名?

(我不能假设一个固定的域名,因为用户来自不同的子域名。)

这假定results是从DirectorySearcher获取的SearchResultCollection,但您应该能够直接从DirectoryEntry获取objectsid。

 SearchResult result = results[0]; var propertyValues = result.Properties["objectsid"]; var objectsid = (byte[])propertyValues[0]; var sid = new SecurityIdentifier(objectsid, 0) var account = sid.Translate(typeof(NTAccount)); account.ToString(); // This give the DOMAIN\User format for the account 

不幸的是,你无法在DirectoryEntry中找到你想要的东西。

您有sAMAccountName ,通常类似于myuser (没有域)。 您有distinguishedName ,类似于LDAP://cn=joe myuser,cn=Users,dc=yourCompany,dc=com 。 您还有一个userPrincipalName但它通常是joeUser@mycompany.com格式的joeUser@mycompany.com

但遗憾的是,您找不到任何包含domain\MyUser属性。 您必须将有关域名的信息和DirectoryEntry的sAMAccountName放在一起。

有关System.DirectoryServices中所有LDAP和WinNT属性的更多信息和一些优秀的Excel表格,请访问ADSI MVP Richard Mueller的Hilltop Lab网站。

要获取DirectoryEntry域名,可以在directoryEntry.Parent上使用递归。 然后,如果directoryEntry.SchemaClassName == "domainDNS"您可以获得如下域名:

 directoryEntry.Properties["Name"].Value 

我在CN = Partitions,CN = Configuration中找到了一个包含所有域的分区容器。

当您将用户与分区匹配时,您可以从nETBIOSName +“\”+ sAMAccountName属性中读取真实域名。

 public static string GetDomainNameUserNameFromUPN(string strUPN) { try { WindowsIdentity wi = new WindowsIdentity(strUPN); WindowsPrincipal wp = new WindowsPrincipal(wi); return wp.Identity.Name; } catch (Exception ex) { } return ""; } 

如果您使用的是System.DirectoryServices库,则应该具有DirectorySearcher中的SearchResultsCollection。

在每个SearchResult的Properties集合中,都有一个“distinguishedname”属性。 这将包含构成目录条目所属域的所有DC部分。

我正在扩展@laktak之前的回答,以提供他的意思的详细信息。

CN=Partitions,CN=Configuration中有一个分区容器,其中包含所有域,它们为您提供了Netbios域名的cn ,以及包含用户在此域中的distinguishedName前缀的nCName属性。

因此,首先在CN=Partitions,CN=Configuration搜索ldap for (objectClass=*)并将每个结果的( cnnCName )对存储到地图中。

接下来,使用(sAMAccountName=USERIDHERE)查询ldap并从用户获取distinguishedName 。 现在浏览( cnnCName )对并找到用户nCNamedistinguishedName前缀的nCName ,相应的cn是您想要的域名。

1)您可以从DirectoryEntry获取userPrincipalName。

2)然后,在用户名和域名之间拆分UPN。

3)然后在其上调用GetNetBIOSName()。

  public static DirectoryEntry GetDirectoryObject(string strPath) { if (strPath == "") { strPath = ConfigurationManager.AppSettings["LDAPPath"]; //YOUR DEFAULT LDAP PATH ie. LDAP://YourDomainServer } string username = ConfigurationManager.AppSettings["LDAPAccount"]; string password = ConfigurationManager.AppSettings["LDAPPassword"]; //You can encrypt and decrypt your password settings in web.config, but for the sake of simplicity, I've excluded the encryption code from this listing. } catch (Exception ex) { HttpContext.Current.Response.Write("user: " + username + ", LDAPAccount: "+ ConfigurationManager.AppSettings["LDAPAccount"] + ".
"+ ex.Message +"
"); if (HttpContext.Current.User.Identity != null) { HttpContext.Current.Response.Write("HttpContext.Current.User.Identity: " + HttpContext.Current.User.Identity.Name + ", " + HttpContext.Current.User.Identity.IsAuthenticated.ToString() + "
"); HttpContext.Current.Response.Write("Windows Identity: " + WindowsIdentity.GetCurrent().Name + ", " + HttpContext.Current.User.Identity.IsAuthenticated.ToString()); } else { HttpContext.Current.Response.Write("User.Identity is null."); } HttpContext.Current.Response.End(); } DirectoryEntry oDE = new DirectoryEntry(strPath, username, password, AuthenticationTypes.Secure); return oDE; } public static string GetNetBIOSName(string DomainName) { string netBIOSName = ""; DirectoryEntry rootDSE =GetDirectoryObject( "LDAP://"+DomainName+"/rootDSE"); string domain = (string)rootDSE.Properties[ "defaultNamingContext"][0]; // netBIOSName += "Naming Context: " + domain + "
"; if (!String.IsNullOrEmpty(domain)) { //This code assumes you have a directory entry at the /CN=Partitions, CN=Configuration //It will not work if you do not have this entry. DirectoryEntry parts = GetDirectoryObject( "LDAP://"+DomainName+"/CN=Partitions, CN=Configuration," + domain); foreach (DirectoryEntry part in parts.Children) { if ((string)part.Properties[ "nCName"][0] == domain) { netBIOSName += (string)part.Properties[ "NetBIOSName"][0]; break; } } } return netBIOSName; } public static string GetDomainUsernameFromUPN(string strUPN) { string DomainName; string UserName; if (strUPN.Contains("@")) { string[] ud = strUPN.Split('@'); strUPN= ud[0]; DomainName = ud[1]; DomainName=LDAPToolKit.GetNetBIOSName(DomainName); UserName= DomainName + "\\" + strUPN; } else { UserName= strUPN; } return UserName; }