function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vance Kessler 23Vance Kessler 23 

Determine if User is accessing via a Trusted IP address

In a Lightning controller, I need to determine if a user is in one of the Trusted IP address ranges. I could not find a way to retrieve the ranges, so I created a metadata setting to hold those values.

The problem now is that I cannot figure out what the user's ip address is to compare against that list of ranges.

How do I retrieve the current User's Ip Address in a Lightning component controller? Better yet, is there a setting/field/tea leaves I could check through Apex to tell if me the user is a trusted user?

I found the code below, but it appears to be for VisualForce pages because ApexPages.CurrentPage is always null in a component.
public static String GetUserIPAddress() {
	string returnValue = null;
	if (null != ApexPages.currentPage() ) {
		Map<String, String> headers = ApexPages.currentPage().getHeaders();
		returnValue = headers.get('True-Client-IP');

		if (returnValue == null) {
			returnValue = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
		} // get IP address when no caching (sandbox, dev, secure urls)
	}
	system.debug('USER IP ADDRESS: ' + returnValue);
	return returnValue;
}