What's the best way to get the currently logged in username (accessing publisher/dispatcher)

jayv25585659 07-01-2020
Session session = getRequest().getResourceResolver().adaptTo(Session.class);

UserManager userManager = getResource().adaptTo(UserManager.class);
Authorizable auth = userManager.getAuthorizable(session.getUserID());

userId = auth.getID();

 

So I have the code above to get the  currently logged in username in my local AEM instance. 

 

In my testing, session.getUserID() = auth.getID(). I feel that using session.getUserID() should be sufficient in trying to get the username of the currently logged in user.

 

In our stage and production environments, we have SSO setup and we're using Okta as our provider.

 

Is there anything I need to change in my code to make it better? Thanks

Accepted Solutions (0)

Answers (0)