Retrieve full name from Active Directory using C# and .NET 4

I have used asp.net application in VS 2010 .net framework 4.0

Steps:

1.  Add reference : Right click Reference folder->Add Reference->DirectoryServices + DirectoryServices.AccountManagement

2. Add system namespace:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

Note: I just add both to avoid any further problem. Usually one can solve the problem.

2. Inside Page_Load(object sender, EventArgs e)

            System.Security.Principal.IPrincipal User;
            User = System.Web.HttpContext.Current.User;
            string opl = User.Identity.Name;
            string username = GetFullName(opl);
            CurrentUser.Text = "Hello " + username;

3. Add GetFullName (Reff-1) Function:

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;
        }



Reff-1: http://milanl.blogspot.com/2008/08/retrieve-full-name-from-active.html

Reff-2: http://weblogs.asp.net/scottgu/archive/2006/07/12/Recipe_3A00_-Enabling-Windows-Authentication-within-an-Intranet-ASP.NET-Web-application.aspx
Advertisements

One comment on “Retrieve full name from Active Directory using C# and .NET 4

  1. Thanks for this post! I am tasked with generating reports off a database where the username is saved as Domain\username. The reports needed the username displayed as the users full name. Using the above I was able to easily generate it! Simply inserted the database field name instead.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s