London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

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

Vote:
 

Hello,

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.

#256629
Jun 15, 2021 5:20
Vote:
 

Try this-

System.Web.HttpContext.Current.User.Identity.Name

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

#256633
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
Vote:
 

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

#256634
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.