• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 873
  • Last Modified:

Active Directory - basics to pull up user info

Hello Experts,

I know a bit about asp.net programming, but I know basically nothing about active directory.  I've tried looking this up online, but haven't had any success.  Could someone post a snippet of code that would lets say display all users?

Thanks,
GRCHELPDESK
0
GRCHELPDESK
Asked:
GRCHELPDESK
1 Solution
 
DavidTMooreCommented:
This might not be exactly what you are looking for, but these are the methods I use to interact with active directory, take a look and maybe they can give you a better understanding of how some of this works.
public static DirectoryEntry GetDirectoryEntry()
    {
        DirectoryEntry de = new DirectoryEntry("LDAP://19dc01", "USERNAME", "PASSWORD");
        return de;
    }
 
 
    public bool UserExists(string username)
    {
        DirectoryEntry de = GetDirectoryEntry();
        DirectorySearcher deSearch = new DirectorySearcher();
 
        deSearch.SearchRoot = de;
        deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))";
 
        SearchResultCollection results = deSearch.FindAll();
 
        return results.Count > 0;
    }
 
    public String GetProperty(String userAccount, String propertyName)
    {
        DirectoryEntry entry = GetDirectoryEntry();
        String account = userAccount.Replace(@"DOMAIN\", "");
 
        try
        {
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(SAMAccountName=" + account + ")";
            search.PropertiesToLoad.Add(propertyName);
 
            SearchResult result = search.FindOne();
 
            if (result != null)
            {
                return result.Properties[propertyName][0].ToString();
            }
            else
            {
                return "Unknown User";
            }
        }
        catch (Exception ex)
        {
            string debug = ex.Message;
            return "";
        }
    }

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now