SaaS CMS has officially launched! Learn more now.

EPiServer.Security.PrincipalInfo.CurrentPrincipal.Identity.Name returns null or empty



Am trying to fetch the username and role for website and returns null for current user. I want to check if logged in user belong to role WebAdmins or Administrator so that they can have access to one of the functionality which others can't see. I added like this:

bool currentUserIsWebAdmins = System.Web.Security.Roles.IsUserInRole("WebAdmins");

 bool currentUserIsAdministrator = System.Web.Security.Roles.IsUserInRole("Administrators");
            if (currentUserIsWebAdmins && currentUserIsAdministrator)
                // code goes here

string currentUser = EPiServer.Security.PrincipalInfo.CurrentPrincipal.Identity.Name; when I checked the currentuser it's null.

Is this correct way to fetch currentusername for a website and am I missing any configuration?

Please help.

Jun 15, 2021 5:20

Try this-


or if you are using Membership then you can use - will contain userid.

Edited, Jun 15, 2021 7:26
Deepa Puranik - Jun 15, 2021 10:48
Tried this thank you for help
Deepa Puranik - Jun 15, 2021 10:48
Tried this thank you for help
Deepa Puranik - Jun 15, 2021 10:48
Tried this thank you for help

For the roles this should work: EPiServer.Security.PrincipalInfo.CurrentPrincipal.IsInRole("WebAdmins")

Jun 15, 2021 7:31
Quan Mai - Jun 15, 2021 7:38
second this
Deepa Puranik - Jun 15, 2021 8:43
If I want to check this logic in Global.asax file, is it possible to do?
Jeroen Stemerdink - Jun 15, 2021 8:51
No, as that's the "startup", and no one is logged in yet
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.