LDAP路径问题

问题描述:

我正在使用LDAP,我对此很陌生。LDAP路径问题

有没有办法获得理想的域名时,你只知道用户名,密码,服务器名

我试图做到这一点:

string ldapPath = "LDAP://serverName"; 
string uid = username; 
string password = pwd; 
string qry = String.Format("(uid={0})", uid); 
string adsPath = String.Empty; 

try 
{ 
    DirectoryEntry nRoot = new DirectoryEntry(ldapPath, null, null, AuthenticationTypes.Anonymous); 

    DirectorySearcher ds = new DirectorySearcher(nRoot, qry); 
    SearchResult sr = ds.FindOne(); 

    if (sr != null) 
    { 
     // we want to retrieve the DN like this: "uid=myuser,ou=People,dc=findlay,dc=edu 
     ldapPath = sr.Path; //update where we will bind next 
    } 

这并不工作,除非我改变

string ldapPath = "LDAP://serverName"; 

string ldapPath = "LDAP://serverName/DC=mydomain,DC=com"; 

任何帮助.. ??

感谢

编辑RootDSE的

string defaultNamingContext; 

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE", null, null, AuthenticationTypes.Anonymous)) 
{ 
    defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString(); 
} 

我也觉得这是解决方案,但目前没有工作对我来说..请帮助!

+0

可能重复[从LDAP目录上下文对象查找BASE DN](http://*.com/questions/7777067/find-base-dn-from-ldap-directory-context-object) – EJP

+0

它是不均匀的标记为已回答..请改为帮忙。谢谢 – user175084

+0

但它*是*回答了两次。 – EJP

你可以尝试这样的

//方法调用

string netBiosName = GetNetBiosName(LDAP://CN=Partitions,CN=Configuration,DC=<DomainName>,DC=<local|com>, "<userName"", "<password>"); 

//方法调用

//方法定义

private string GetNetBiosName(string ldapUrl, string userName, string password) 
{ 
    string netbiosName = string.Empty; 
    DirectoryEntry dirEntry = new DirectoryEntry(ldapUrl,userName, password); 

    DirectorySearcher searcher = new DirectorySearcher(dirEntry); 
    searcher.Filter = "netbiosname=*"; 
    searcher.PropertiesToLoad.Add("cn"); 

    SearchResultCollection results = searcher.FindAll(); 
    if (results.Count > 0) 
    { 
    ResultPropertyValueCollection rpvc = results[0].Properties["CN"]; 
    netbiosName = rpvc[0].ToString(); 
    } 
    return netbiosName; 

} 

请在此链接看看for more info

+0

问题是我不知道域名。我只知道服务器名称,用户名和密码。 – user175084

您应该可以通过调用RootDse来获取域。

+0

我也试过这个解决方案,但我得到一个错误:对象引用未设置为对象的实例。事情是我连接到一个Linux服务器。我更新了我的问题 – user175084

+0

我假设你也尝试了“LDAP:// RootDSE”而不是“LDAP:// servername/rootdse”?我没有想法,从来没有以这种方式连接到Linux服务器。 – Lareau

+0

是的,我有..谢谢你的帮助 – user175084

RootDSE不是服务器绑定 - 试试这个:

string defaultNamingContext; 

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://rootDSE", null, null, AuthenticationTypes.Anonymous)) 
{ 
    defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString(); 
} 

或者,如果你在.NET 3.5和更高版本,可以使用PrincipalContext相反,它可以不带任何路径构造 - 它只是拿起你连接到默认域:

PrincipalContext context = new PrincipalContext(ContextType.Domain); 

你应该检查出System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里阅读所有关于它(这是.NET 3。5及更高版本):

如果:

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/DC=mydomain,DC=com") 
{ 
    ... 
} 

的作品,你有没有尝试(没有被匿名):

string defaultNamingContext; 

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE") 
{ 
    defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString(); 
} 

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE", user, password) 
{ 
    defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString(); 
} 

它适用于我,从不在域中的计算机。