views:

982

answers:

4

I want to create a quick application for people to resolve the name of a user stored in Active Directory from a set of credentials. Some applications only provide the user id and it is too much to expect an end user to fire up the Active Directory Users and Groups MMC snap-in.

Input would be something like "MYCORP\a_user" and output would be "Dave Smith" if that is what is stored in AD.

I want this to be able to run in my test domain and also in a multi-forest environment.

Can someone provide a sample that does this? Does retrieval of other attributes from AD such as telephone number follow the same pattern?

Target platform: .NET 2.0 and above.

+1  A: 

You'll need to use code found in the System.DirectoryServices namespace. This article should get you started.

Greg Hurlman
A: 

See DirectorySearcher, loading the property "DisplayName".

sfuqua
...and filtering on SamAccountName (but only by using the user name, after the "domain\" part)....
marc_s
+3  A: 

Here's the code I use, taken from my authentication class:

string[] strUserName = username.Split("\\".ToCharArray());
using (var entry = new DirectoryEntry("LDAP://" + ADServer, ADServiceDomain + "\\" + ADServiceAccount, ADServicePassword))
using (var ds = new DirectorySearcher(entry, "sAMAccountName=" + strUserName[1])) {
  ds.SearchScope = SearchScope.Subtree;
  SearchResult result = ds.FindOne();
  string fullname = result.Properties["displayName"][0].ToString();
}


System.DirectoryServices sucks. As you can see, it takes a ridiculous amount of code to do even the most basic things. I'd like to see a user authentication method that didn't require using exceptions for flow control.

Adam Lassek
Be careful as there are some known memory leaks associated with ActiveDirectory objects if not used correctly..http://englestone.blogspot.com/2008/06/active-directory-memory-leaks-c.html-- Lee
Lee Englestone
Good to know. Updated with using statements.
Adam Lassek
+2  A: 

Working with Active Directory is a bit painfull in C#, sure 3.5 adds some new classes to help, but for pure productivity I like to use Powershell and Quest's free PowerShell Commands for Active Directory in which case the code looks something like

get-qaduser userid | select PhoneNumber,DisplayName

if you need this to run as part of your C# program, you can do that too

    public static IEnumerable<PSObject> Invoke(string script, params object[] input)
    {
        IList errors = null;
        using (var run = new RunspaceInvoke())
        {
            var psResults = run.Invoke(script, input, out errors);
            if (errors != null && errors.Count > 0)
                Debug.WriteLine(errors.Count);
            foreach (PSObject res in psResults)
                yield return res;
        }
    }
    PSObject psUser = POSHelp.Invoke(
        @"add-pssnapin Quest.ActiveRoles.ADManagement
        ($userid) = $input | % { $_ }
        get-qaduser $userid", "auserid").Single();
     Debug.WriteLine(psUser.Properties["DisplayName"].Value);

add a ref to Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

Scott Weinstein