locked
Active Directory Users RRS feed

  • Question

  • User1180806401 posted

    Hi Guys,

    I'm using the below code to load users from the Active Directory and its working for all clients in different servers (Win 2008 server, Win 2012 R2 server) but with this last client the code can't find most properties (displayname, sn,givenname, mail) and could only find samaccountname, so does it mean my client didn't setup the users correctly ?

     

    string[] userDetails = Context.User.Identity.Name.Split('\\');

    //get user login domain
    string domainName = userDetails[0].ToString();
    DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
    if (deRoot != null)
    domainName = deRoot.Properties["defaultNamingContext"].Value.ToString();
    Utilities.ShowAlert(UpdatePanel1, ScriptManager1, domainName, "ID");

    DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + domainName);

    DirectorySearcher search = new DirectorySearcher(directoryEntry);
    search.Filter = "(&(objectClass=user)(objectCategory=person))";
    search.PropertiesToLoad.Add("displayname");
    search.PropertiesToLoad.Add("samaccountname");
    search.PropertiesToLoad.Add("sn");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("givenName");

    SearchResult result;
    SearchResultCollection resultCol = search.FindAll();

    System.Web.UI.WebControls.ListItem emptyItem = new System.Web.UI.WebControls.ListItem(" -- Select User Name -- ", "");
    activeDirectoryUsersDropDownList.Items.Add(emptyItem);

    if (resultCol != null)
    {

    for (int counter = 0; counter < resultCol.Count; counter++)
    {
    string UserNameEmailString = string.Empty;
    result = resultCol[counter];
    if (result.Properties.Contains("samaccountname") &&
    result.Properties.Contains("sn") &&
    result.Properties.Contains("displayname") &&
    result.Properties.Contains("givenName"))

    {
    string displayname = (String)result.Properties["displayname"][0];
    string userName = (String)result.Properties["samaccountname"][0];
    string sn = (String)result.Properties["sn"][0];
    string givenName = (String)result.Properties["givenName"][0];
    string userData = givenName + "|" + sn + "|" + "" + "|" + displayname;

    System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(userName + " - (" + displayname + ")", userData);
    activeDirectoryUsersDropDownList.Items.Add(item);

    }
    }

    Tools.SortByText(activeDirectoryUsersDropDownList);

    Tnanks

    Thursday, August 8, 2019 10:19 AM

All replies