May 23, 2021

How to find whether impersonated as a user using "Log in to Experience as User" in Salesforce?


Visualforce Page:
<apex:page controller="CommunityPageController">
    Logged in As User? {!loggedInAsUserBool}
</apex:page>

Apex Controller:
public class CommunityPageController {
    
    public Boolean loggedInAsUserBool { get; set; }
    
    public CommunityPageController() {
    
        loggedInAsUserBool = false;        
        Map < String, String > currentSessionInfo = Auth.SessionManagement.getCurrentSession();
        
        if ( currentSessionInfo.get( 'LoginHistoryId' ) == null &&
           currentSessionInfo.get( 'LoginType' ) == 'Unknown' &&
           currentSessionInfo.get( 'LogoutUrl' ) == null ) {
               
           loggedInAsUserBool = true;
           
       }
        
    }

}

Logged in As Experience User(without impersonation):


Admin Logged in As Experience User(with impersonation):


No comments:

Post a Comment