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

Posted on 2012-08-21
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

    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.


    Author Closing Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now