Retrieve IP Address for the Guest Users in Lightning Community
Recently, we received a requirement where we needed to track the IP address of the guest users on the Salesforce Lightning Community.
One of the ways to get the IP address of the current user in Apex is using the following method:
But this method works only for logged in users and gives an error for the guest users. We tried getting the IP address from the cookies also, but it's not a good idea because it is really not necessary that for guest users, you will get the IP address in the cookies always.
At last, we came up with the following workaround to solve this riddle:
1) Create the following apex controller (ViewIPAddressController):
2) Now, create the following Visualforce page (ViewIPAddress):
3) Now, use below piece of code wherever you want to get the IP Address of the guest user, it can be any apex class or @AuraEnabled method in the Lightning Component's Controller:
One of the ways to get the IP address of the current user in Apex is using the following method:
Auth.SessionManagement.getCurrentSession().get('SourceIp')
But this method works only for logged in users and gives an error for the guest users. We tried getting the IP address from the cookies also, but it's not a good idea because it is really not necessary that for guest users, you will get the IP address in the cookies always.
At last, we came up with the following workaround to solve this riddle:
1) Create the following apex controller (ViewIPAddressController):
public class ViewIPAddressController{
public String ipAddress{get; set;}
public ViewIPAddressController(){
//this will work if no caching is in place or
//user is logged in via secure URL
ipAddress = ApexPages.currentPage().getHeaders().
get('True-Client-IP').get('X-Salesforce-SIP');
//this logic will execute if proxy is in use
if(String.isEmpty(ipAddress)){
ipAddress = ApexPages.currentPage().getHeaders().
get('True-Client-IP').get('X-Forwarded-For');
}
}
}
2) Now, create the following Visualforce page (ViewIPAddress):
<apex:page controller="ViewIPAddressController" showHeader="false">
<ipAddress>{!ipAddress}</ipAddress>
</apex:page>
3) Now, use below piece of code wherever you want to get the IP Address of the guest user, it can be any apex class or @AuraEnabled method in the Lightning Component's Controller:
String ipAddress = (new PageReference('/apex/ViewIPAddress')).
getContent().toString().
substringBetween('<ipAddress>', '</ipAddress>');
So, using this small workaround, we can easily implement this tedious requirement. Hope this is helpful.
Happy Coding!!
Hi - thanks so much for posting this. I've been trying to figure out how to do this for a while! One question from someone who is basically not a coder. How can I use this information in a flow? Thanks!
ReplyDeleteTrying this myself, the IP that is resolved is Salesforce's server rather than the client's machine. Am I missing something?
ReplyDeleteHi - Thanks for posting this. I am able to get the IP using VF page but how can we achieve it in lightning component?
ReplyDeleteLet me know if you did this. Thank You.