Showing posts from September, 2019

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: 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().getHeade