Get User IP Address in a Canvas App



How to Get User IP Address in a Canvas App Using PCF


Background

Power Apps doesn’t natively provide a way to directly retrieve the IP address of the client within a Canvas app. However, we can achieve this by creating a custom PCF component. In this blog, I’ll guide you through the process using an existing PCF control called “Get Client Details.”

Prerequisites

  1. Power Apps Environment: Make sure you have access to a Power Apps environment where you can create and manage apps.
  2. Get Client Details Control: Since the IP is retrieved via a PCF Control you need to have it in your app. You can download it from here.

Steps

  1. Download the Solution

    • I’ve pre-built the solution for you. You can download it from here.
    • Import the downloaded solution into your Power Apps environment by navigating to the Solutions area in the maker portal and clicking the “Import solution” button.


  1. Add the PCF Control to Your Canvas App

    • Open your Canvas app.
    • Click on Insert > Get more components.


    • Head to the Code tab and select the Whitelist component.

      If you don't se the Code tab,

      1. Sign in to Power Apps.
      2. Click on Settings and then select Admin Center.
      3. In the left pane, choose Environments and select the environment where you want to enable this feature.
      4. Click on Settings within the environment.
      5. Expand the Product section and select Features.
      6. From the list of available features, turn on Power Apps component framework for canvas apps, and then click Save.

      Once you’ve enabled this feature, you’ll be able to add code components to your canvas apps.



    • Once imported, the control will appear in the Insert panel on the left.
    • Drag and drop it onto your canvas.
  1. Display the IP Address

    • The control will now display the client’s IP address.
    • If you don’t want the IP address to be visible to the user, you can set the Visible property of the GetClientDetails control to false.


  1. Save the IP Address to a Variable

    • To process or save the IP address, reference the Output property of the Whitelist control.
    • For example, you can use the following formula to save the IP address to a variable called varIPAddress:
      Set(varIPAddress, GetClientDetails1.IPAddress)
      


How the “Get Client Details” Control Works

The “Get Client Details” control makes a call to an external web resource that returns the client’s IP address.

Conclusion

By implementing the “Get Client Details” control, you can enhance security in your Power Apps by validating user access based on their IP addresses. Feel free to explore other PCF components or customize this one further to meet your specific requirements.


Comments