LDAP Connection Error
I have a problem connecting to my ldap. It keeps giving me a COMExceptionError (The parameter is incorrect)
Here is the code i have so far:
static void Main(string[] args)
{
DirectoryEntry ldapConnection = new DirectoryEntry("10.9.130.113:667");
ldapConnection.Path = "LDAP://ou=Users,ou=CorporateStore,ou=Absa,c=za";
ldapConnection.AuthenticationType = AuthenticationTypes.Anonymous;
DirectorySearcher ds = new DirectorySearcher(ldapConnection);
SearchResult result = ds.FindOne();
Console.ReadLine();
if (result 开发者_运维技巧!= null)
{
ResultPropertyCollection fields = result.Properties;
foreach (String ldapField in fields.PropertyNames)
{
foreach (Object myCollection in fields[ldapField])
Console.WriteLine(String.Format("{0,-20} : {1}",
ldapField, myCollection.ToString()));
Console.ReadLine();
}
This is the line the error occurs at:
SearchResult result = ds.findOne();
Heres the exception Error and stack trace:
System.Runtime.InteropServices.COMException was unhandled
Message=The parameter is incorrect.
Source=System.DirectoryServices
ErrorCode=-2147024809
StackTrace:
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at LDAPConnector.Program.Main(String[] args) in c:\documents and settings\expn261\my documents\visual studio 2010\Projects\LDAPConnector\LDAPConnector\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Any ideas?
Try the following:
- If your LDAP server is AD then you must perform a bind on the connection since AD doesn't allow anonymous connections.
- As far as I understood your are trying to connect via SSL, so try to connect without SSL first (default port 389), also try to specify the addres in the following format "ldaps://10.9.130.113:667".
- You don't need "LDAP://" prefix in the
ldapConnection.Path
- Before using search try to perform simplier operations like simple bind to narrow the problem.
You have to specifies some properties to load for findone() method to work. In this sample try to find properties of a user (username is a strig variable).
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain); //domain is a string with the FQDN (ex: int.domain.local) or alias (es: mydomainname)
DomainControllerCollection dcc = DomainController.FindAll(context);
DirectorySearcher ds;
ds = dcc[0].GetDirectorySearcher();
ds.Filter = String.Format("(&(sAMAccountName={0})(objectClass=user))", username);
ds.PropertiesToLoad.Add("lastLogon");
ds.PropertiesToLoad.Add("displayName");
ds.PropertiesToLoad.Add("memberOf");
ds.PropertiesToLoad.Add("userAccountControl");
ds.PropertiesToLoad.Add("ADSPath");
ds.PropertiesToLoad.Add("PrimaryGroupID");
ds.PropertiesToLoad.Add("pwdLastSet");
ds.PropertiesToLoad.Add("maxPwdAge");
ds.PropertiesToLoad.Add("mail");
ds.PropertiesToLoad.Add("distinguishedName");
ds.PropertiesToLoad.Add("mdbstoragequota");
ds.PropertiesToLoad.Add("SamAccountName");
ds.SizeLimit = 15;
SearchResult sr = ds.FindOne();
It seems like you define different paths in the constructor to the DirectoryEntry and then override it by setting the Path property. If your server differ from the domain in the RDN you shall define it in the path. Can you try to do it in this way and see if you get a different error?
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://10.9.130.113:667/ou=Users,ou=CorporateStore,ou=Absa,dc=za");
And skip the part where you setting the path via the property.
EDIT: NOTICE It also seems like you have missed a "d" on the dc=za.
精彩评论