Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have to connect to local ldap which is on apache server. I use your code like this

C#
private string sDomain = "ldap://localhost:10389";
   private string sDefaultOU = "ou=users,ou=system";

   private string sServiceUser = @"uid=admin,ou=system";
   private string sServicePassword = "secret";

PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, sServiceUser, sServicePassword);


But I didn't get connected, anything wrong I am doing.
Posted
Updated 24-Aug-12 11:40am
v2

Its bit late to give you my solution - but It would be useful for others if the encounter the same issue.

Possible solution would be as follows
C#
private string sDomain = "localhost:10389";
//sDomain is ServerName or Hosename or just IP address
//You had it with ldap://

private string sDefaultOU = "ou=users,ou=system";

private string sServiceUser = @"uid=admin,ou=system";
private string sServicePassword = "secret";

PrincipalContext oPrincipalContext = new PrincipalContext
   (ContextType.Domain, sDomain, sDefaultOU, sServiceUser, sServicePassword);


Hope this helps and it worked for me.
 
Share this answer
 
You are not passing the right parameters.
The five parameter constructor overload's third parameter is not default OU, it is default container! Thus you need to qualify it with DC-s. And the user name parameter need not be qualified with OU, use just the "admin" string.
Check also here: Using System.DirectoryServices.AccountManagement[^]
 
Share this answer
 
Comments
mayankkarki 27-Aug-12 3:34am    
Thanks for reply,
But I didn't get succeed.I tried to connect with michigan open ldap server

private string sDomain = "LDAP://ldap.itd.umich.edu:389";
private string sDefaultOU = "ou=System Groups,ou=Groups,dc=umich,dc=edu";
private string sServiceUser = "cn=Directory Manager,o=University of Michigan,c=us";
private string sServicePassword = "";

First I use this and works perfectly
DirectoryEntry de = new DirectoryEntry(sDomain + "/" + sDefaultOU, sServiceUser, sServicePassword,AuthenticationTypes.ServerBind);
DirectorySearcher ds = new DirectorySearcher(de);
SearchResult sr = ds.FindOne();

But with principalcontext got same error "server could not be contacted."
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain,sDefaultOU,ContextOptions.ServerBind|ContextOptions.Negotiate, sServiceUser, "");
Is there something missing?
Zoltán Zörgő 27-Aug-12 5:08am    
For PrincipalContext, the sDomain should be only the dns name (no protocol, no port).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900