Active Directory in C#.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmj07
    New Member
    • Aug 2008
    • 55

    Active Directory in C#.NET

    Hi,

    I need some help with my Active Directory coding in C#. All I am trying to do is quey the active directory on my server to return information about a user such as first name, surname etc. Here is my code at the moment:

    Code:
    DirectoryEntry de = new DirectoryEntry("LDAP://ServersIPAddress"); 
    
    DirectorySearcher srch = new DirectorySearcher(de);
    
    srch.Filter = "(&(objectClass=user)(|(cn=PersonsUsername)))";
    
    SearchResult result = srch.FindOne();
    
    DirectoryEntry directoryObject = new DirectoryEntry();
    
    directoryObject = result.GetDirectoryEntry();
    With this code the program stops at the directoryObject line saying:

    Object reference not set to an instance of an object

    Any ideas please?

    Many thanks in advance.
Working...