Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Last active January 9, 2019 21:36
Show Gist options
  • Save SalesforceBobLightning/0300d833a1887033df41558f6998fc77 to your computer and use it in GitHub Desktop.
Save SalesforceBobLightning/0300d833a1887033df41558f6998fc77 to your computer and use it in GitHub Desktop.
Salesforce Apex Networking Utils
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