Blog

PCF - How to get the IP address of the client

If you need to find out the IP address of the client in a canvas app, this post walks through how to accomplish this task with the help of a PCF control.

On occasions, I see questions from app builders who attempt to obtain the IP of the current user (such as the one beneath).


There's no built-in way to retrieve the IP address from a canvas app. However, we can obtain the IP address by creating a PCF component. MVP Yash Agarwal has a 'Whitelist IP Address' control that's freely available through the PCF Gallery site, and this post walks through how to use this control.

It's also worth noting that there's a post in the ideas forum that suggests the addition of IP detection in Power Apps. You can vote for this here if you would like this added to Power Apps.

How does the 'Whitelist IP Address' control work and where to find the source code?

We can find the source code for the 'Whitelist IP Address' control through the following GitHub project site.





This control works by making a call to the following web resource:
https://ifconfig.me/all.json

This endpoint returns the IP address of the client as shown below:

An important thing to note is that because this control calls an external web resource, it's a premium control and therefore requires a premium Power Apps license.

How to install the IP Address Control

To save the task of building the control and solution from Github, I've pre-built the solution which and you can download from here:

After downloading the 'whitelistSoln.zip' file, you can import it into your Power Apps environment by navigating to the Solutions area of the maker portal and clicking the 'Import solution' button.


How to detect the IP Address from a Canvas App

From a canvas app, we can now add the control by clicking Insert > Get more components, selecting the 'Code' tab and selecting the 'Whitelist' component.


Once we import the control, it will appear in the 'Insert' panel on the left. We can drag this onto our canvas and the control will show the IP address.



How to save the IP address of the client to a variable

Rather than display the IP address on the screen, we generally need to extract the IP address so that we can process it or save it to a data source. We can carry out this task by referencing the OutPut property of the control like so:

Set(varIPAddress, Whitelist1.OutPut)
This formula saves the IP address to a variable called varIPAddress and we can use this to reference the IP address.


A useful tip is that we can set the visible property of the Whitelist1 control to false if we don't want it to appear to the user.

Conclusion

On occasions where we need to retrieve the IP address of the client, we can accomplish this task with the help of a PCF control. This post walked through how to carry out this task. An important thing to note is that this solution will require either a Power Apps per-user or a per-app license.
  •   Categories: 
  • pcf