![](/img/trans.png)
[英]How do i get a list of all users, passwords and groups from an activeDirectory using System.DirectoryServices?
[英]How to get domain alias using System.DirectoryServices.ActiveDirectory.Domain class
我们有一个全名的域名,例如long-domainname.com ; 此域名替换为别名short 。 可以使用netapi32.dll
检索此别名,如下所示:
[DllImport("Netapi32.dll")]
static extern int NetApiBufferFree(IntPtr Buffer);
// Returns the domain name the computer is joined to, or "" if not joined.
public static string GetJoinedDomain()
{
int result = 0;
string domain = null;
IntPtr pDomain = IntPtr.Zero;
NetJoinStatus status = NetJoinStatus.NetSetupUnknownStatus;
try
{
result = NetGetJoinInformation(null, out pDomain, out status);
if (result == ErrorSuccess &&
status == NetJoinStatus.NetSetupDomainName)
{
domain = Marshal.PtrToStringAuto(pDomain);
}
}
finally
{
if (pDomain != IntPtr.Zero) NetApiBufferFree(pDomain);
}
if (domain == null) domain = "";
return domain;
}
此方法返回排序值。 但是使用System.DirectoryServices.ActiveDirectory.Domain
class 及其Name
属性,我得到了 long-domainname.com值。 在调试模式下搜索属性,我找不到任何短值字段或属性。 System.DirectoryServices.ActiveDirectory.Domain
class 有可能吗? 或者可能与System.DirectoryServices
名称空间的其他一些 class 一起使用? 如何在不导入外部*.dll的情况下获取短域名值?
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;
}
接受的答案有效,但有两个问题可以解决:
这是在 one.network 请求中执行此操作的方法:
private string GetNetbiosDomainName(string dnsDomainName) {
var domain = new DirectoryEntry($"LDAP://{dnsDomainName}"); //bind to the root of the domain
domain.RefreshCache(new [] { "msDs-PrincipalName" });
return ((string)domain.Properties["msDs-PrincipalName"].Value).Trim('\\');
}
它确实要求你运行它的帐户受到你正在查找的域的信任(不一定在同一个林中),但你甚至可以通过在DirectoryEntry
的构造函数中传递凭据来避免这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.