locked
Problems getting new DirectoryEntry prior to search of AD RRS feed

  • Question

  • User-3424553 posted

    Here is a sanitized version of the code I am trying to get working

    The call in Main is to 

    DirectoryEntry myLdapConnection = createDirectoryEntry();

    static DirectoryEntry createDirectoryEntry()
    {
    // create and return new LDAP connection with desired settings

    String myADSPath = "LDAPS://server.bozo.clown.com:636/OU=365Users,DC=bozo,DC=clown,DC=com";
    DirectoryEntry ldapConnection = new DirectoryEntry(myADSPath, @"adminuser@bozo.clown.com", @"whatever12_Password", AuthenticationTypes.Secure);
    return ldapConnection;
    }

    When I try to make the call for DirectoryEntry de =new DirectoryEntry(adsPath,username,
    password,AuthenticationTypes.Secure) it returns a DirectoryEntry object, ldapConnection,
    but many of the attributes in debugger for the value, ldapConnection, returned, show this type of thing:


    'ldapConnection.AdsObject' threw an exception of type
    'System.Runtime.InteropServices.COMException'
    'ldapConnection.ContainerObject' threw an exception of
    'System.Runtime.InteropServices.COMException'
    type'ldapConnection.Guid' threw an exception of type
    'System.Runtime.InteropServices.COMException'
    'ldapConnection.IsContainer' threw an exception of type
    'System.Runtime.InteropServices.COMException'

    Same for Name, NativeObject, NativeGuid,Object Security....

    Examining this object using VS2010....

    Do anyone have any relevant references or advice on this problem and how to resolve it? (code snippets most welcome)

    Tuesday, October 17, 2017 5:53 PM

Answers

  • User-3424553 posted

    OK, I finally got this working. As one respondent suggested, I used the newer System.DirectoryServices.AccountManagement, instead of the older DirectoryServices methods.

    I was just trying to get the PrincipalContext without errors like server could not be contacted. I know it was my arguments to the constructor and finally figured out the correct syntax. In case, anyone else is struggling with this.....[I have to use the secure LDAP port 636 for my AD server]....below is a snippet. Substitute your values and I hope it works for you as it just worked for me.

    string sDomainServer = "BOZO.CLOWN.COM:636"; // works
    string sContainer = "DC=bozo,DC=clown,DC=com";
    string sServiceUser = "bozo" + @"\" + "username";
    string sServicePassword = @"some_crazy_password";
    PrincipalContext aPC = new PrincipalContext(ContextType.Domain, sDomainServer, sContainer, ContextOptions.SimpleBind, sServiceUser, sServicePassword);

    I was able to verify that the PrincipalContext was working by doing this...and getting result.

    using (PrincipalContext oPC = new PrincipalContext(ContextType.Domain, sDomain, sContainer, ContextOptions.SimpleBind, sServiceUser, sServicePassword))
    {

          UserPrincipal qbeUser = UserPrincipal.FindByIdentity(oPC, IdentityType.SamAccountName, "pegduffy");

          ....

    }

    Thanks to everyone who replied. Now to get all users in this domain

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 19, 2017 6:37 PM

All replies

  • User753101303 posted

    Hi,

    And your final goal is ? If querying AD for users you also have the "new" https://msdn.microsoft.com/en-us/library/bb384379(v=vs.100).aspx  namespace which is easier to use.

    Or you want to query a non AD directory using LDAP ?

    Tuesday, October 17, 2017 6:18 PM
  • User-3424553 posted

    My eventual goal is to create a .csv file with all users in our O365 Active Directory who have mailboxes enabled. With that list of users, I need to get their proxyaddress, primary SMTP address and other attributes. I know I can do these things with powershell, but occasionally, once per week, the powershell program that does a similar thing, times out...never comes back. The program in PS runs every 30 minutes, M-F from 8-5...and it fails about once a week. I was hoping to replace powershell calls with a program to create a flat file and read that data into a datatable.....

    Tuesday, October 17, 2017 6:26 PM
  • User1771544211 posted

    Hi  peg.duffy,

    The following link shows how to query LADP to get the user information using C#, please take it as reference.

    https://stackoverflow.com/questions/7721696/c-sharp-ldap-query-to-retrieve-all-users-in-an-organisational-unit

    Best Regards,

    Jean

    Wednesday, October 18, 2017 5:19 AM
  • User753101303 posted

    I didn't know you could use LDAP for Office 365. Your PS script uses which commands?

    For this I would likely use rather the Microsoft Graph API : https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list

    You have a nuget package that provides a client side library.

    If you want to keep the current approach what is the first exception you have when using actual code. I wouldn't try to diagnose an exception that happens when using the debugger viewer.

    Wednesday, October 18, 2017 8:10 AM
  • User-3424553 posted

    OK, I finally got this working. As one respondent suggested, I used the newer System.DirectoryServices.AccountManagement, instead of the older DirectoryServices methods.

    I was just trying to get the PrincipalContext without errors like server could not be contacted. I know it was my arguments to the constructor and finally figured out the correct syntax. In case, anyone else is struggling with this.....[I have to use the secure LDAP port 636 for my AD server]....below is a snippet. Substitute your values and I hope it works for you as it just worked for me.

    string sDomainServer = "BOZO.CLOWN.COM:636"; // works
    string sContainer = "DC=bozo,DC=clown,DC=com";
    string sServiceUser = "bozo" + @"\" + "username";
    string sServicePassword = @"some_crazy_password";
    PrincipalContext aPC = new PrincipalContext(ContextType.Domain, sDomainServer, sContainer, ContextOptions.SimpleBind, sServiceUser, sServicePassword);

    I was able to verify that the PrincipalContext was working by doing this...and getting result.

    using (PrincipalContext oPC = new PrincipalContext(ContextType.Domain, sDomain, sContainer, ContextOptions.SimpleBind, sServiceUser, sServicePassword))
    {

          UserPrincipal qbeUser = UserPrincipal.FindByIdentity(oPC, IdentityType.SamAccountName, "pegduffy");

          ....

    }

    Thanks to everyone who replied. Now to get all users in this domain

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 19, 2017 6:37 PM