Earlier for a VF page, We were getting the ip address of the current user from visualforce page standard headers. But we cant use the same in Lightning controller.

Does anyone know any solution to get the IP address in apex of a lightning page?


2 Answers 2


Can you just not query the AuthSessoin object and there you can find the source IP address.

Link to the document - AuthSession

SELECT Id, SourceIp FROM AuthSession WHERE UsersId = :UserInfo.getUserId ORDER BY CreatedDate DESC LIMIT 1

I thing that the best way to do it is:

 public static String getUserIPAddress() {
    Map<String, String> session = Auth.SessionManagement.getCurrentSession();
    return session.get('SourceIp');

Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Auth_SessionManagement.htm

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.