Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm new in writing code to access AD. I setup following AD structue:
DC=UAT,DC=COM
OU=ORG
OU=Users and Groups
OU=System
OU=Groups
CN=Admins
OU=Users
OU=Test
OU=Groups
OU=Users
CN=test01
CN=test02

I wrote the following sample codes to search for the user test02, but got error:
using System;
using System.IO;
using System.DirectoryServices;
using System.Configuration;
try
{
    DirectoryEntry userEntry = new DirectoryEntry("LDAP://SERVERIP/OU=ORG,DC=UAT,DC=COM", user, password); 
    DirectorySearcher search = new DirectorySearcher(userEntry);
             
    search.SearchScope = SearchScope.Subtree

    // Search user test02           
    search.Filter = "(&(objectClass=user)(objectCategory=Person)(sAMAccountName=test02))";               
             
    SearchResult result = search.FindOne();   // Failed at this line,  throw exception 

    if (result == null)
       Console.WriteLine("There is no results to display");
    else
       Console.WriteLine("Found result ");                
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.Message);
    Console.WriteLine("Trece: " + ex.StackTrace);
}


Error:  Configuration system failed to initialize<br />
Trece:    at  System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)<br />
   at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)<br />
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)<br />
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)<br />
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)<br />
   at System.DirectoryServices.SearchResultCollection.ResultsEnumerator..ctor(SearchResultCollection results, String parentUserName, String parentPassword, AuthenticationTypes parentAuthenticationType)<br />
   at System.DirectoryServices.SearchResultCollection.GetEnumerator()<br />
   at System.DirectoryServices.DirectorySearcher.FindOne()<br />


Could you help and advise what I did wrong?

Thank you very much!
Lwang
Posted
Updated 31-Mar-15 7:09am
v2
Comments
Richard Deeming 31-Mar-15 15:36pm    
Looks like you have an error in your application's configuration file.
Member 11570433 31-Mar-15 16:34pm    
Could you provide me more information on what might be setup wrong in my application's configuration file. Or if you send me a sample configuration file, that will be much appreciated. Thanks!

Lwang

1 solution

I figured out my configuration file error. Thanks!
 
Share this answer
 

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