Whitelisting IP addresses in Bulk in the PowerApps Portals Admin Center

Jared Johnson, 08 March 2021

In the PowerApps Portals admin center you are able to set up an allow list of IP Addresses that can access the portal via the Set up IP address restriction option.

image

You can only add 1 address at a time, so what if you have to add a large number, like a few hundred or more?

Using the Network Monitor in the developer tools (via pushing F12) we can see that adding an IP address does a POST to https://portaladmin-oce.portal-infra.dynamics.com/IPAddressFilter/AddIPAddress (url will be different depending on region). We can now take this request so that we can repeat it for the rest of the IP addresses we need to do, you can right click the request and go to Copy -> Copy as Fetch and that will give us everything we need to run this request in the browser console.

I have included some sample code below that is built around the Fetch request and using it to loop over an array of IP addresses. To use it first update the code with values from your Fetch request – the admin portal url, requestverificationtoken and portalID. Then take your IP Addresses and add them to the IPAddresses array, something like this converter can be helpful for doing this. Then you can copy it into the Developer Console, hit enter and then type Whitelist.Go(). Note that in the body there is an addressType value, this is either 1 for IPv4 or 2 for IPv6. I have left it hardcoded as 1 in this sample but if you have IPv6 addresses you will need to update this to 2.

if (typeof Whitelist === typeof undefined) {

    Whitelist = {};

}

function timeout(ms) {

    return new Promise(resolve => setTimeout(resolve, ms))

}

Whitelist = {

    SendRequest: async function (ip) {

        console.log("Processing Address: ", ip)

        fetch(`${AdminPortalUrl}/IPAddressFilter/AddIPAddress`, {

            "headers": {

                "__requestverificationtoken": `${RequestVerificationToken}`,

                "accept": "application/json, text/plain, */*",

                "content-type": "application/json;charset=UTF-8",

                "pragma": "no-cache", 

                "request-context": "appId=cid-v1:80397749-9e56-4d16-80c9-2894b0299946",

                "request-id": "|Svd+N.yVbV0",

               "sec-ch-ua-mobile": "?0" ,

                "sec-fetch-dest": "empty" ,

               "sec-fetch-mode": "cors" ,

                "sec-fetch-site": "same-origin",

                "x-requested-with": "XMLHttpRequest" 

            },

           "referrer": `${AdminPortalUrl}/?tenantProductId=${PortalId}&lcid=1033&geo=OCE`,

            "referrerPolicy": "strict-origin-when-cross-origin",

            // addressType is 1 for IPv4 and 2 for IPv6

            "body": `{"ipAddress":"${ip}","addressType":1, portalId":"${PortalId}"}`,

            "method": "POST",

           "mode": "cors" , 

            "credentials": "include"

        });

    },

    Go: async function () {

        for (i = 0; i < IpAddresses.length; i++) {

            console.log("Processing Address: ", IpAddresses[i])

            Whitelist.SendRequest(IpAddresses[i])

            await timeout(5000)

        }

    },

}

// Replace the below settings for your environment.

// Set one request while developer console open in your browser, copy out the request, then extract the token. 

RequestVerificationToken = "VEhJU0lTQUZBS0VSRVFVRVNUVE9LRU5ZT1VTSUxMWUJJTExZ" ; 

// Located in your URL query string 

PortalId = "00000000-0000-0000-0000-000000000001";

AdminPortalUrl = https://portaladmin-oce.portal-infra.dynamics.com;

// Take all your IP Addresses and add them to this array

IpAddresses = [

    "55.55.55.55/16",

   "66.66.66.66/16",

    "25.25.25.25/30",

    "75.75.75.75/29"

]