I am trying to dynamically determine, in an organization-agnostic fashion, whether Apex is executing in an authenticated environment --- whether Customer Portal, Partner Portal, Salesforce Communities, or regular logins. Basically, anything except for an unauthenticated Force.com Site login.

My understanding is that if the Apex running user does NOT have the Site Guest User license, then you are in an authenticated environment. Therefore, querying the User object for the related Profile's UserLicense record should provide this capability:

boolean isAuthenticated = [
   select Profile.UserLicense.LicenseDefinitionKey 
   from User 
   where Id = :UserInfo.getUserId() limit 1
].Profile.UserLicense.LicenseDefinitionKey != 'GUEST';

Ideally, I would like to avoid having to use a SOQL query just to get this information. Is there any other way to get this, without having to use a SOQL query? Ideally there'd be some Apex system class or programmatic check that I could run that would guarantee that I'm in an authenticated environment or not.

  • UserInfo.isCurrentUserLicensed() ? May 28, 2013 at 20:18
  • @MartinBorthiry you have to call it with a parameter and it doesn't like null or empty string...
    – eyescream
    May 28, 2013 at 22:04

2 Answers 2


Winter '15 introduced the Auth.CommunitiesUtil class which contains a isGuestUser() for determining if the user is a guest or not.


Can you explore the Site class methods? I don't have Portal handy to test but Site.getName() returns null in regular "Execute Anonymous" window so it seems to be a good starting point.

Maybe something like

Boolean isAuthenticatedContext = String.isBlank(Site.getName()) ||
Site.isLoginEnabled() || Site.isRegistrationEnabled();

(no idea what do these last 2 methods return when you're on "pls register me" Site).

Another way - authenticated user should have a session Id. So maybe

Boolean isAuthenticatedContext = String.isNotBlank(UserInfo.getSessionId()) &&
!System.isBatch() && !System.isFuture() && !System.isScheduled();
  • Unfortunately, neither of these methods work. (1) I have explored the Site class methods and have not found anything that would work, as users can still access a Force.com Site that's Login/Registration Enabled in unauthenticated mode, so the fact that a Site has an associated portal does not necessitate that the running user IS logged in. (2) does not work because Site Guest users DO have a Session Id, I have checked.
    – zachelrath
    May 29, 2013 at 13:53
  • I'm out of ideas for now :/
    – eyescream
    May 29, 2013 at 14:05
  • Is there any difference in the content of Cookies or HTTP headers being sent on authenticated vs. unauthenticated Site? I'd imagine such method being more prone to future changes though...
    – eyescream
    May 29, 2013 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.