by: Jørgen Helgheim
09 March 2009
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</span>
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:
<span class="csharpformat">UserData.FirstName + “ “ + UserData.LastName + “(“ + UserData[“PersonalUserName”] +”)” ;</span>
//So as you can see, you need to use the indexer to retrieve the username.
<span class="csharpformat">string username = currentUser.UserData[“PersonalUserName”];