Last active
January 9, 2019 21:36
-
-
Save SalesforceBobLightning/0300d833a1887033df41558f6998fc77 to your computer and use it in GitHub Desktop.
Salesforce Apex Networking Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public without sharing class NetworkingUtils { | |
public static String getCurrentUserIPAddress() { | |
Map<String, String> headersMap = ApexPages.currentPage().getHeaders(); | |
// True-Client-IP has the value when the request is coming via the caching integration. | |
String ipAddress = headersMap.get('True-Client-IP'); | |
if (String.isEmpty(ipAddress)) { | |
// X-Salesforce-SIP has the value when no caching integration or via secure URL. | |
// get IP address when no caching (sandbox, dev, secure urls) | |
ipAddress = headersMap.get('X-Salesforce-SIP'); | |
} | |
if (String.isEmpty(ipAddress)) { | |
// get IP address from standard header if proxy in use | |
ipAddress = headersMap.get('X-Forwarded-For'); | |
} | |
return ipAddress; | |
} | |
public static String getSourceIPAddress() { | |
return Auth.SessionManagement.getCurrentSession().get('SourceIp'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment