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?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityEarlier 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?
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 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