Get the Long User Name of a Logged-in Account within C#

Posted on 2012-08-21
Medium Priority
Last Modified: 2012-08-23
I'm looking for a way to find the name of the user logged in and running a C# application.

I found that
string username = Environment.UserName;

Open in new window

works to find the username: "jsmith".

What I want to find is a name in the format: "John Smith".
Question by:bpatton_psr
LVL 53

Accepted Solution

Dhaest earned 1500 total points
ID: 38319274
If you're using .Net 3.0 or higher, there's a lovely library that makes this practically write itself. System.DirectoryServices.AccountManagement has a UserPrincipal object that gets exactly what you are looking for and you don't have to mess with LDAP or drop to system calls to do it. Here's all it'd take:

WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal; 
// or, if you're in Asp.Net with windows authentication you can use: /
/ WindowsPrincipal principal = (WindowsPrincipal)User; 
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain)) 
     UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
     return up.DisplayName;
     // or return up.GivenName + " " + up.Surname;

Open in new window

Note: you don't actually need the principal if you already have the username, but if you're running under the users context, it's just as easy to pull it from there.

Source: http://stackoverflow.com/questions/3471635/get-first-name-last-name-of-logedin-windows-user

Author Closing Comment

ID: 38326246
This is the code I ended up using:
UserPrincipal up = UserPrincipal.Current;
fullName = up.GivenName + up.Surname;

The most difficult thing I found was that you need to add a reference to the .NET package System.DirectoryServices.AccountManagement by right clicking on References and clicking Add Reference.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question