/// <summary>
 /// Compares two specified LdapUser objects and returns an integer that indicates their relationship to one another in the sort order.
 /// </summary>
 /// <param name="x">The first LdapUser instance.</param>
 /// <param name="y">The second LdapUser instance.</param>
 /// <returns>
 /// A 32-bit signed integer indicating the lexical relationship between the two comparands.
 /// Value Condition Less than zero x.UserName is less than y.UserName.
 /// Zero x.UserName equals y.UserName.
 /// Greater than zero x.UserName is greater than y.UserName.
 /// </returns>
 public int CompareLdapUsers(LdapUser x, LdapUser y)
 {
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     else
     {
         if (y == null)
         {
             return(1);
         }
         else
         {
             return(string.Compare(x.UserName, y.UserName));
         }
     }
 }
        private LdapUser InternalGetUser(SearchResult searchResult)
        {
            if (searchResult == null)
            {
                return(null);
            }

            LdapUser result = new LdapUser();

            if (searchResult.Properties["samAccountName"].Count > 0)
            {
                result.UserName = searchResult.Properties["samAccountName"][0].ToString();
            }

            if (searchResult.Properties["displayName"].Count > 0)
            {
                result.DisplayName = searchResult.Properties["displayName"][0].ToString();
            }

            if (searchResult.Properties["mail"].Count > 0)
            {
                result.EmailAddress = searchResult.Properties["mail"][0].ToString();
            }

            if (searchResult.Properties["department"].Count > 0)
            {
                result.Department = searchResult.Properties["department"][0].ToString();
            }

            if (searchResult.Properties["telephoneNumber"].Count > 0)
            {
                result.PhoneNumber = searchResult.Properties["telephoneNumber"][0].ToString();
            }

            if (searchResult.Properties["pwdLastSet"].Count > 0)
            {
                result.PasswordLastSetTime = DateTime.FromFileTime(Convert.ToInt64(searchResult.Properties["pwdLastSet"][0]));
            }

            result.Groups = new List <string>();

            if (searchResult.Properties["MemberOf"] != null)
            {
                foreach (object obj in searchResult.Properties["MemberOf"])
                {
                    result.Groups.Add(this.GetCommonName((string)obj));
                }
            }

            result.MailingAddress = new MailingAddress();

            string streetAddress = null;

            if (searchResult.Properties["streetAddress"].Count > 0)
            {
                streetAddress = searchResult.Properties["streetAddress"][0].ToString();
            }

            if (!string.IsNullOrEmpty(streetAddress))
            {
                int newLineIndex = streetAddress.IndexOf(Environment.NewLine);

                if (newLineIndex == -1)
                {
                    result.MailingAddress.StreetLine1 = streetAddress;
                }
                else
                {
                    result.MailingAddress.StreetLine1 = streetAddress.Substring(0, newLineIndex);
                    result.MailingAddress.StreetLine2 = streetAddress.Substring(newLineIndex + 2, streetAddress.Length - newLineIndex - 2);
                }
            }

            if (searchResult.Properties["postOfficeBox"].Count > 0)
            {
                result.MailingAddress.POBox = searchResult.Properties["postOfficeBox"][0].ToString();
            }

            if (searchResult.Properties["l"].Count > 0)
            {
                result.MailingAddress.City = searchResult.Properties["l"][0].ToString();
            }

            if (searchResult.Properties["st"].Count > 0)
            {
                result.MailingAddress.State = searchResult.Properties["st"][0].ToString();
            }

            if (searchResult.Properties["postalCode"].Count > 0)
            {
                result.MailingAddress.PostalCode = searchResult.Properties["postalCode"][0].ToString();
            }

            if (searchResult.Properties["c"].Count > 0)
            {
                result.MailingAddress.Country = searchResult.Properties["c"][0].ToString();
            }

            return(result);
        }