• Publisert
  • 1 min

UnifiedPrincipal.Current retreive UserName

Keywords: EPiServer 4.6, UserName, EPiServer.Security.UnifiedPrincipal.Current

I had this code, and I needed to know the username of the currently logged on user.

Using EPiServer.Security;
UnifiedPrincipal currentUser = UnifiedPrincipal.Current; //gets the current user object

Surprisingly the PersonalizedData object, currentUser.UserData, didn’t contain any property for this. currentUser.UserData.DisplayName property includes the username, but you can’t be sure that is retrieved only the username. If FirstName and LastName is filled in on the users page in admin mode, DisplayName will return:

UserData.FirstName + “ “ + UserData.LastName + “(“ + UserData[“PersonalUserName”] +”)” ;

//So as you can see, you need to use the indexer to retrieve the username.

string username = currentUser.UserData[“PersonalUserName”];