4

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?

1

2 Answers 2

5

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
1
  • How would this work if the use case was to get this info from a public website hosted on Salesforce Sites? I don't think that this would work (as the public users won't be authenticated). I guess using Google Analytics or a similar tool like Cloudflare Web Analytics would have to be used. Jan 25 at 0:17
3

I think 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .