简体   繁体   中英

LDAP path problems

I am working with LDAP and I am new to this.

Is there a way to get the domain when you only know the username, password, servername

I am trying to do this:

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
    }

This does not work unless I change

 string ldapPath = "LDAP://serverName";

to

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

Any help..??

Thanks

Edit rootDSE

string defaultNamingContext;

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

I too feel this is the solution but it is currently not working for me.. please help!

you can try like this

// Method call

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

// Method call

// Method Definition

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;

}

pls take a look at this link for more info

您应该只需调用RootDse即可获取域名。

RootDSE is not server-bound - try this:

string defaultNamingContext;

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

Or if you're on .NET 3.5 and newer, you could use PrincipalContext instead, which can be constructed without any path - it will just pick up the default domain you're connected to:

PrincipalContext context = new PrincipalContext(ContextType.Domain);

You should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here (that's .NET 3.5 and newer):

If :

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

works, have you try (without being anonymous):

string defaultNamingContext; 

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

or

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

It works for me, from a computer not in the domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM