Thursday, August 21, 2008

Retrieve full name from Active Directory using C#

Once upon a time, I was given a task to build a simple attendance system to replace the attendance register in our office. Starting from sourceforge.net, I tried to find open source application to fit our requirement. After spending few hours in the Internet, I could not convinced myself with the applications that I came across. Then I suddenly realized that we are using Active Directory to log in to domain and also thought that if I could get the user detail from AD somehow, I can build a system which does not require any user login. Finally, I was able to find the code to get the full name of the current Windows user and build a small prototype for attendance system. The logic behind this application is to get the user detail from Active Directory and simply log attendance detail to another database. This will reduce the overhead of loggin in to the system as well as user administration part. Below is the code to get the full name of Windows user;
using System.DirectoryServices;

public static string GetFullName(string strLogin)
    {
        string str = "";
        string strDomain;
        string strName;

        // Parse the string to check if domain name is present.
        int idx = strLogin.IndexOf('\\');
        if (idx == -1)
        {
            idx = strLogin.IndexOf('@');
        }

        if (idx != -1)
        {
            strDomain = strLogin.Substring(0, idx);
            strName = strLogin.Substring(idx + 1);
        }
        else
        {
            strDomain = Environment.MachineName;
            strName = strLogin;
        }

        DirectoryEntry obDirEntry = null;
        try
        {
            obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
            System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
            object obVal = coll["FullName"].Value;
            str = obVal.ToString();
        }
        catch (Exception ex)
        {
            str = ex.Message;
        }
        return str;
    }

12 comments:

Anonymous said...

I am doing the exact same thing for a .NET web application. This helped a bunch.

Your coding style is very nice and neat...easy to read and understand.

Many Thanks!

-Eric

Anonymous said...

Hi!

You can solve it only in one line.

return System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

DSeres

sg said...

System.DirectoryServices.AccountManagement is only available for .net v3.5 (or greater)

Nirjhar said...

Hi! I just want to thank you. This solution rocked :) thankx

Marcus S said...

DSeres:

Your oneliner actually returns the full name of the "iisnetworkaccess"-account and not the full name of the logged in user.

So if it works for you I suppose there are cases when it doesn't work. That probably makes the method to get the full name through User.Identity.Name and the GetFullName-function a more stable one.

Krishna S said...

exactly this is what i was searching for.

btw what is diff between WinT and LDAP protocol ???

sali said...

many thanks Milan,
I have solved this in this way using DirectoryService and also using DirectoryEntry namspaces ......
Shahbaz ali
From Pakistan.

Riaan de Lange said...

Thanks!

Alex Oragwu said...

my computer kept returning a name without the "\" prefix. Everyone on my network uses the same domain so I manually put in the domain as "".

It worked perfect otherwise.

Thank you!

Lee Searcy said...

I searched for over 3 hours to find something like this... Thank you very much for putting this out there. I wish I could have found it sooner.

Props to you!

Satyabrata Mahapatra said...

Hello,
How to call this GetFullName(string strLogin) method?

Thank you!

Milan said...

Satyabrata, you can use the above method in your code and simply call it by passing the Windows login id as a parameter. It will then fetch the full name of that user from Active Directory.