如果我对这个主题的理解有一些缺陷,请原谅我,我只知道我对域和事件目录的了解,因为我从与它们的合作中学到了一些东西。
域名有两个不同的“版本”。第一个是我所说的 DNS 域名,类似于 company.int (对于用户 [email protected] ),第二个类似于 prefixname(对于用户 prefixname\max),它们都指的是同一事物。
我的问题是,给定“company.int”,如何将其转换为“prefixname”?
编辑:或者给定一个 System.DirectoryServices.ActiveDirectory.Domain 对象,如何获取前缀名称?
EDIT2:此外,“前缀名称”还有其他名称吗?我一直不知道该怎么调用它。
EDIT3:我试图获取的值与 Windows 登录屏幕上显示的“登录到”(其中列出了域和您的计算机)上显示的值相同。
EDIT4:我发现我可以通过执行以下操作来获取该值:
SecurityIdentifier sid = GetCurrentUserSID();
string prefixName = sid.Translate(typeof(NTAccount)).Value.Split('\\')[0];
有人知道更好的方法来获得这个名字吗?
最佳答案
我希望这应该可以:
private string GetNetbiosDomainName(string dnsDomainName)
{
string netbiosDomainName = string.Empty;
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string configurationNamingContext = rootDSE.Properties["configurationNamingContext"][0].ToString();
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://cn=Partitions," + configurationNamingContext);
DirectorySearcher searcher = new DirectorySearcher(searchRoot);
searcher.SearchScope = SearchScope.OneLevel;
searcher.PropertiesToLoad.Add("netbiosname");
searcher.Filter = string.Format("(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))", dnsDomainName);
SearchResult result = searcher.FindOne();
if (result != null)
{
netbiosDomainName = result.Properties["netbiosname"][0].ToString();
}
return netbiosDomainName;
}
您基本上用“mydomain.com”来调用它,并且应该返回 netbios 域名,例如“MYDOMAIN”(通常)。
马克
关于.net - 如何从dns域名中获取短 "domain name"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1214512/