locked
converting old active directory code to new namespace references RRS feed

  • Question

  • User-1510887245 posted

    I am updating some CS code that was written in 2004, and that references some assemblies which extract user data from  AD by query filtering on the user's title.  I can't test my updated code in VS2010 because the assemblies no longer compile - the references are no longer valid (I can't create a DirectoryEntry or DirectorySearcher)  so I need to work out how to replace the code with current directoryservices.accountmanagement namespace properties and methods.  I have searched around but don't seem to be getting anywhere. 
    Can anyone give me some code that will work with vs 2010 to replicate the following:

        DirectoryEntry oDE = new DirectoryEntry("LDAP://xxx/dc=abc,dc=def,dc=au", ConfigurationSettings.AppSettings["ADUserName"], ConfigurationSettings.AppSettings["ADPassword"]);
        DirectorySearcher oDS = new DirectorySearcher(oDE);
        oDS.SearchScope = SearchScope.Subtree;
        oDS.PageSize = 0;

        //Create the DataTable to return the results in
        DataTable oQueryResults = CreateDataTable(false, enmType.User);
       
        //The DataTable contains the fields we want out of AD, so
        //only get these fields out of AD.
        foreach(DataColumn dc in oQueryResults.Columns) {
         oDS.PropertiesToLoad.Add(dc.Caption);
        }

        //Add the filter
        oDS.Filter = "(&(objectCategory=person)(objectClass=user)(title=" + jobtitle + "))";

        //Build the results
        BuildDataTable(ref oQueryResults, oDS.FindAll());
       
        oDE.Close();
        oDE.Dispose();
        oDS.Dispose();
       
        //Return the results
        return oQueryResults;

     

    Thanks in advance !!

    Thursday, June 24, 2010 7:44 PM

All replies

  • User1169449775 posted

    If you have access to .Net 3.5, I've found the System.DirectoryServices.AccountManagement class to be VERY easy to use.

    I'll take a stab at duplicating what you're requesting below in S.DS.AM

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ABC", "dc=abc,dc=def,dc=au", 
      ConfigurationSettings.AppSettings["ADUserName"], ConfigurationSettings.AppSettings["ADPassword"]);
      // There are several different ways of calling this...play around, see what works
    
    using (ctx) {
      // Now inside here is where you do your lookup of properties, based on what's in your database
      // However, you can extend the UserPrincipal (I'm referring to MyPrincipal below as an extended UserPrincipal)
      // with a custom object to include whatever properties you want.  
      // These will be populated automatically from the AD when you query it.  I'll use a base
      // Query By Example (QBE) method below, to illustrate the jobtitle filter you've included.
      
      MyPrincipal toFind = new MyPrincipal(ctx);
      toFind.JobTitle = jobTitle;
      
      PrincipalSearcher ps = new PrincipalSearcher(toFind);
      MyPrincipal  found = ps.FindOne() as MyPrincipal;
      
      // I haven't done the FindAll() in what I'm working on, but I'm assuming you could readily
      // do this to get it to work
      
      var results = ps.FindAll();
      foreach (MyPrincipal found in results) {
        // Insert them into your DataTable, Grid, whatever
      }
      
    }


    Hope this gives you an idea of what you were looking for.

    Friday, July 9, 2010 4:34 PM
  • User-1510887245 posted

    Thankyou.   As it was, after encouragement from someone who said it should definitely work, I basically started from scratch and re-created the bits that weren't working and got DirectoryEntry and DirectorySearcher working.

    I would like to try the System.DirectoryServices.AccountManagement class so thanks for the starter code. I will give it a go.

    Sunday, July 11, 2010 7:51 PM