2011-10-16 275 views
1

我正在使用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://stackoverflow.com/questions/7777067/find-base-dn-from-ldap-directory-context-object) – EJP

+0

它是不均勻的標記爲已回答..請改爲幫忙。謝謝 – user175084

+0

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

回答

1

你可以嘗試這樣的

//方法調用

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

1

您應該可以通過調用RootDse來獲取域。

+0

我也試過這個解決方案,但我得到一個錯誤:對象引用未設置爲對象的實例。事情是我連接到一個Linux服務器。我更新了我的問題 – user175084

+0

我假設你也嘗試了「LDAP:// RootDSE」而不是「LDAP:// servername/rootdse」?我沒有想法,從來沒有以這種方式連接到Linux服務器。 – Lareau

+0

是的,我有..謝謝你的幫助 – user175084

1

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及更高版本):

1

如果:

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(); 
} 

它適用於我,從不在域中的計算機。