locked
C# and LDAP query RRS feed

  • Question

  • User653873109 posted

    Hi, i am trying to get some properties from ldap for the current user logged on. i have the samAccountname but i want to be able to pull the persons position as well but i am not sure how to pull this from LDAP. i have the foolowing code;

     

     string user = Request.ServerVariables["AUTH_USER"].ToString();
                string[] splituser = user.Split( '\\' );

                DirectorySearcher search = new DirectorySearcher( de );
                search.Filter = "(SAMAccountName=" + splituser[1] + ")";
                search.PropertiesToLoad.Add( "displayName" );
                search.PropertiesToLoad.Add("position");

                SearchResult result = search.FindOne();

                if (result != null)
                {
                    tbName.Text = result.Properties["displayname"][0].ToString();
                    tbPosition.Text = result.Properties["position"][0].ToString();
                }
                else
                {
                    tbName.Text = "";
                    tbPosition.Text = "";
                }

     but this gives me an arguments out of bound exception, would be greatful if anyone can advise please!

    Wednesday, July 14, 2010 7:00 AM

Answers

  • User1508394307 posted

    Ok, I see. So, it seems that that object has no "position" attribute. Do you maybe need to get the "title" attribute?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 14, 2010 4:45 PM

All replies

  • User1508394307 posted

    it's probably here: search.Filter = "(SAMAccountName=" + splituser[1] + ")"; 

    Check what you have in the splituser variable.

    Wednesday, July 14, 2010 8:18 AM
  • User653873109 posted

    hi, thanks for replying.

     

    i get the arguments exception at the tbposition row not at the search filter as you suggested. any thoughts? 

    Wednesday, July 14, 2010 9:02 AM
  • User1508394307 posted

    Ok, I see. So, it seems that that object has no "position" attribute. Do you maybe need to get the "title" attribute?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 14, 2010 4:45 PM
  • User653873109 posted

    hi, thanks for your advice

    i think it was the "title" attribute rather than position. my code as follows: 

    tbName.Text = result.Properties["displayname"][0].ToString();
    tbPosition.Text = result.Properties["title"].ToString();

     

    however when this runs, i get the following message in the position texbox: "system.DirectoryServices.ResultpropertyValueCollection" when i just want the title of their position.

    what am i doing wrong?
               

    Thursday, July 15, 2010 10:46 AM
  • User1508394307 posted

    I think you still need to add [0] to get the first element from the returned value. Don't forget to add search.PropertiesToLoad.Add("title");  to ensure that this property is loaded and add following lines to check if value is presented:

    if(result.Properties.Contains("title"))
    tbPosition.Text = result.Properties["title"][0].ToString();
    else 
    tbPosition.Text = "";

    If all this will still not work, use LDAP Browser (or any other tool) to check what properties you should be able to get for the user.


    Thursday, July 15, 2010 11:11 AM
  • User15685074 posted

    There is a much easier way to do all of this in .NET 3.5 SP1, you no longer have to build complex queries.  Reference these classes:

    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;

    You can see them in use with this open source project: http://umanage.codeplex.com

    Brent 

    Sunday, July 18, 2010 1:18 AM