Click here to Skip to main content
Click here to Skip to main content

Get a User's Full Name

, 4 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Use .NET 3.5's DirectoryServices namespace to quickly and easily get at a user's full name.

Introduction

The System.DirectoryServices.AccountManagement namespace in .NET 3.5 gives you access to the UserPrincipal class which "Encapsulates principles that are user accounts." There is a .Current property which "Gets a user principal object that represents the current user under which the thread is running." Once you have this UserPrincipal object for the current user, you can get at all kinds of cool properties and methods.

Background 

I wanted to display the current user's full name to see who is using my application and then log that to a table in SQL, how hard could it be!? I was using VB.NET for this particular project so I tried My.User.Name... whew that was pretty easy. Once run, it returned "MyDomain\UsersInitials" since the username standard we have here are the initials. Getting the initials was not very helpful in figuring out who was using my program (I'm too lazy to manually cross reference usernames, with full names in ADUC).

So I had to search around for quite a while and I couldn't find any articles or postings that quickly point to this class for user information. There were other people that had this question and they were turned to everything from using "WinNT://" queries, to using the Win32_UserAccount and the WMI Code Creator, to older articles saying you just can't do it!

Eventually I stumbled upon this article on MSDN.

I figured I'd post an article to maybe make it easier for people to find information on getting at current user properties. 

Using the Code 

Using the code couldn't be easier. Just add a reference and using or imports statement to System.DirectoryServices.AccountManagement and enjoy. 

Imports System.DirectoryServices.AccountManagement

Dim userFullName As String = UserPrincipal.Current.DisplayName

One caveat, as pointed out in a comment below, is that this should be wrapped with a Try...Catch block. If the current user is on a domain and can't reach the domain controller (network failure), he will receive a PrincipalServerDown exception. However if he is not on a domain, he will not receive this error on a network failure.

Also, if you change the username and don't log off, you will receive a NoMatchingPrincipleException since the .Current UserPrincipal name can't be found. This would probably never happen but you might as well Catch it.

If an exception occurs, I'd suggest just using the username from System.Environment.UserName or My.User.Name in VB.

Once you get at the .Current property, which returns a UserPrincipal class, you can access all kinds of properties on the current user. A table of properties is listed below (from here): 

  Name Description
Public property AccountExpirationDate Gets or sets a Nullable DateTime that specifies the date and time that the account expires. (Inherited from AuthenticablePrincipal.)
Public property AccountLockoutTime Gets the Nullable DateTime that specifies the date and time that the account was locked out. (Inherited from AuthenticablePrincipal.)
Public property AdvancedSearchFilter Returns an AdvancedSearchFilter object, for use with Query by Example to set read-only properties before passing the object to the PrincipalSearcher. (Overrides AuthenticablePrincipal..::.AdvancedSearchFilter.)
Public property AllowReversiblePasswordEncryption Gets or sets a Boolean value that specifies whether reversible password encryption is enabled for this account. (Inherited from AuthenticablePrincipal.)
Public property BadLogonCount Gets the number of logon attempts using incorrect credentials for this account. (Inherited from AuthenticablePrincipal.)
Public property Certificates Gets a X509Certificate2Collection that contains the X509 certificates for this account. (Inherited from AuthenticablePrincipal.)
Public property Context Gets a principal context that is associated with the principal. (Inherited from Principal.)
Protected property ContextRaw Gets a principal context that is associated with this principal. (Inherited from Principal.)
Public property ContextType Gets the context type enumeration value that specifies the type of principal context associated with this principal. (Inherited from Principal.)
Public propertyStatic member Current Gets a user principal object that represents the current user under which the thread is running.
Public property DelegationPermitted Gets or sets a Nullable Boolean value that specifies whether the account may be delegated. (Inherited from AuthenticablePrincipal.)
Public property Description Gets or sets the description of the principal. (Inherited from Principal.)
Public property DisplayName Gets or sets the display name for this principal. (Inherited from Principal.)
Public property DistinguishedName Gets the distinguished name (DN) for this principal. (Inherited from Principal.)
Public property EmailAddress Gets or sets the e-mail address for this account.
Public property EmployeeId Gets or sets the employee ID for this user principal.
Public property Enabled Gets or sets a Nullable Boolean value that specifies whether this account is enabled for authentication. (Inherited from AuthenticablePrincipal.)
Public property GivenName Gets or sets the given name for the user principal.
Public property Guid Gets the GUID associated with this principal. (Inherited from Principal.)
Public property HomeDirectory Gets or sets the home directory for this account. (Inherited from AuthenticablePrincipal.)
Public property HomeDrive Gets or sets the home drive for this account. (Inherited from AuthenticablePrincipal.)
Public property LastBadPasswordAttempt Gets the Nullable DateTime that specifies the date and time of the last incorrect password attempt on this account. (Inherited from AuthenticablePrincipal.)
Public property LastLogon Gets the Nullable DateTime that specifies the date and time of the last logon for this account. (Inherited from AuthenticablePrincipal.)
Public property LastPasswordSet Gets the Nullable DateTime that specifies the last date and time that the password was set for this account. (Inherited from AuthenticablePrincipal.)
Public property MiddleName Gets or sets the middle name for the user principal.
Public property Name Gets or sets the name of this principal. (Inherited from Principal.)
Public property PasswordNeverExpires Gets or sets a Boolean value that specifies whether the password expires for this account. (Inherited from AuthenticablePrincipal.)
Public property PasswordNotRequired Gets or sets a Boolean value that specifies whether a password is required for this account. (Inherited from AuthenticablePrincipal.)
Public property PermittedLogonTimes Gets or sets the times when the principal can logon. (Inherited from AuthenticablePrincipal.)
Public property PermittedWorkstations Gets the list of workstations that this principal is permitted to log into. (Inherited from AuthenticablePrincipal.)
Public property SamAccountName Gets or sets the SAM account name for this principal. (Inherited from Principal.)
Public property ScriptPath Gets or sets the script path for this account. (Inherited from AuthenticablePrincipal.)
Public property Sid Gets the Security ID (SID) of the principal. (Inherited from Principal.)
Public property SmartcardLogonRequired Gets or sets a Boolean value that specifies whether a smartcard is required to log on to the account. (Inherited from AuthenticablePrincipal.)
Public property StructuralObjectClass Gets the structural object class directory attribute. (Inherited from Principal.)
Public property Surname Gets or sets the surname for the user principal.
Public property UserCannotChangePassword Gets or sets a Boolean value that specifies whether the user can change the password for this account. Do not use this with a ComputerPrincipal. (Inherited from AuthenticablePrincipal.)
Public property UserPrincipalName Gets or sets the user principal name (UPN) associated with this principal. (Inherited from Principal.)
Public property VoiceTelephoneNumber Gets or sets the voice telephone number for the user principal.

Points of Interest

Some other interesting methods I found in there were ChangePassword, ExpirePasswordNow, GetGroups, UnlockAccount, SetPassword, and Save. I just haven't had the chance to check them out. Thanks .NET 3.5!

History

  • Updated 9/23/09
    • Added spiff on exception handling
    • Updated code to use .DisplayName instead of .Name 

  • Updated 12/07/12
    • Fixed typo to UserPrincipal instead of UserPrinciple.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jabit
Software Developer Veracity
United States United States
I'm a .NET developer, fluent in C# and VB.NET with a focus on SharePoint and experience in WinForms, WPF, Silverlight, ASP.NET, SQL Server. My roots come from a support/system administrator role so I know my way around a server room as well.
 
I have a passion for technology and I love what I do.

Comments and Discussions

 
QuestionHi PingroupNavin Kumar K Subramanian26-Mar-12 17:37 
AnswerRe: Hi Pinmemberjabit27-Mar-12 5:10 
GeneralRe: Hi PingroupNavin Kumar K Subramanian27-Mar-12 17:37 
GeneralRe: Hi Pinmemberjabit28-Mar-12 6:41 
GeneralRe: Hi PingroupNavin Kumar K Subramanian28-Mar-12 18:24 
GeneralRe: Hi PingroupNavin Kumar K Subramanian3-Apr-12 17:55 
GeneralRe: Hi Pinmemberjabit4-Apr-12 12:41 
GeneralRe: Hi PingroupNavin Kumar K Subramanian4-Apr-12 18:01 
GeneralRe: Hi Pinmemberjabit5-Apr-12 13:13 
GeneralRe: Hi Pinmemberjabit28-Mar-12 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 4 Mar 2013
Article Copyright 2009 by jabit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid