locked
Active Directory Query for specific user RRS feed

  • Question

  • User-191123834 posted

    I have read countless forums and found nothing so for so apparently what I need is an unusual request.

    I need a code snippet that will iterate through an AD domain account and display all properties for a given user.

    Has anyone ever built such a script?  

    If there is a code snippet that displays just the available properties of a user object, this would be sufficient.  I am trying to build an application to add users to our LDAP and need to populate all our specific values however I don't have the specific property names to populate. 

    thanks in advance. 

     

    Wednesday, May 19, 2010 8:37 AM

Answers

  • User-456111751 posted

    Use the namespace:  

    using System.DirectoryServices;

    Code to query AD:

    int i = 0;

    string logonname;

    string primarylogon;

    DirectorySearcher search = null;

    SearchResult result = null;

    try

    {

    //this gets us the domain user id or the LAN id

    logonname = Request.ServerVariables["logon_user"].ToString();

    i = logonname.IndexOf("\\");

    primarylogon = logonname.Substring(i + 1).Trim();

    search = new DirectorySearcher();

    search.Filter = String.Format("(SAMAccountName={0})", primarylogon);

    search.PropertiesToLoad.Add("samaccountname");

    search.PropertiesToLoad.Add("displayname");

    search.PropertiesToLoad.Add("mail");

    search.PropertiesToLoad.Add("SN");

    search.PropertiesToLoad.Add("givenName");

    result = search.FindOne();

    if (result == null)

    {

    Session["req_lanid"] = "Not Available";

    Session["req_name"] = "Not Available";

    Session["req_email"] = "Not Available";

    Session["req_Fname"] = "Not Available";

    Session["req_Lname"] = "Not Available";

    }

    else

    {

    try

    {

    Session["req_lanid"] = result.Properties["samaccountname"][0].ToString();

    }

    catch

    {

    Session["req_lanid"] = "";

    }

    try

    {

    Session["req_name"] = result.Properties["displayname"][0].ToString();

    }

    catch

    {

    Session["req_name"] = "";

    }

    try

    {

    Session["req_email"] = result.Properties["mail"][0].ToString();

    }

    catch

    {

    Session["req_email"] = "";

    }

    try

    {

    Session["req_Fname"] = result.Properties["givenName"][0].ToString();

    }

    catch

    {

    Session["req_Fname"] = "";

    }

    try

    {

    Session["req_Lname"] = result.Properties["SN"][0].ToString();

    }

    catch

    {

    Session["req_Lname"] = "";

    }

    }

    }

    catch (Exception ex)

    {

    Response.Write("An error occured while searching the LAN ID for details. " + ex.Message);

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2010 12:41 AM

All replies

  • User-863835478 posted

    Please define "AD" and "LDAP".

    Wednesday, May 19, 2010 12:04 PM
  • User-456111751 posted

    Use the namespace:  

    using System.DirectoryServices;

    Code to query AD:

    int i = 0;

    string logonname;

    string primarylogon;

    DirectorySearcher search = null;

    SearchResult result = null;

    try

    {

    //this gets us the domain user id or the LAN id

    logonname = Request.ServerVariables["logon_user"].ToString();

    i = logonname.IndexOf("\\");

    primarylogon = logonname.Substring(i + 1).Trim();

    search = new DirectorySearcher();

    search.Filter = String.Format("(SAMAccountName={0})", primarylogon);

    search.PropertiesToLoad.Add("samaccountname");

    search.PropertiesToLoad.Add("displayname");

    search.PropertiesToLoad.Add("mail");

    search.PropertiesToLoad.Add("SN");

    search.PropertiesToLoad.Add("givenName");

    result = search.FindOne();

    if (result == null)

    {

    Session["req_lanid"] = "Not Available";

    Session["req_name"] = "Not Available";

    Session["req_email"] = "Not Available";

    Session["req_Fname"] = "Not Available";

    Session["req_Lname"] = "Not Available";

    }

    else

    {

    try

    {

    Session["req_lanid"] = result.Properties["samaccountname"][0].ToString();

    }

    catch

    {

    Session["req_lanid"] = "";

    }

    try

    {

    Session["req_name"] = result.Properties["displayname"][0].ToString();

    }

    catch

    {

    Session["req_name"] = "";

    }

    try

    {

    Session["req_email"] = result.Properties["mail"][0].ToString();

    }

    catch

    {

    Session["req_email"] = "";

    }

    try

    {

    Session["req_Fname"] = result.Properties["givenName"][0].ToString();

    }

    catch

    {

    Session["req_Fname"] = "";

    }

    try

    {

    Session["req_Lname"] = result.Properties["SN"][0].ToString();

    }

    catch

    {

    Session["req_Lname"] = "";

    }

    }

    }

    catch (Exception ex)

    {

    Response.Write("An error occured while searching the LAN ID for details. " + ex.Message);

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2010 12:41 AM
  • Tuesday, May 25, 2010 12:43 AM
  • User-456111751 posted

    Did my Code Worked? 

    Wednesday, May 26, 2010 5:27 AM
  • User-191123834 posted

    Yes it did.  Thank you.  I have what i need for this. 

    Thursday, May 27, 2010 10:37 AM