1

PREFACE: I know how this is done in the general sense, my question is how to best set it up on Azure App Service

a) Where is configuration done for CORS on a Rest Service (App-#1) hosted on Azure App Service? Is it the traditional way via C# code e.g. config.EnableCors(); or can it also be done via Azure Portal magic?

b) If the client App (App-#2) is hosted on (another) Azure App Service, where to find the external IP address which will be sent in Ajax calls to App-#1?

Lex Li
  • 60,503
  • 9
  • 116
  • 147
joedotnot
  • 4,810
  • 8
  • 59
  • 91
  • App Service (Windows) or (Linux)? – Lex Li Mar 11 '23 at 02:36
  • @Lex Li Windows (but is it different with Linux ? You could answer for that as well) – joedotnot Mar 11 '23 at 03:49
  • App Service on Windows is almost IIS/Windows, so existing discussions like https://stackoverflow.com/questions/49450854/how-to-authorize-cors-preflight-request-on-iis-with-windows-authentication covered that. App Service on Linux is a completely different story. – Lex Li Mar 11 '23 at 05:17

1 Answers1

1

Is it the traditional way via C# code e.g. config.EnableCors(); or can it also be done via Azure Portal magic?

Thanks @Lex Li for the comment.

We can configure CORS either from code or from Azure Portal => Deployed App Service.

To enable CORS in Azure App Service, Navigate to the deployed App Service in Azure Portal => select CORS UNDER API.

As mentioned in the MSDoc we can also enable CORS using Azure CLI.

My Initial CORS section:

enter image description here

Run the below command in Azure Cloud Shell.

az webapp cors add --resource-group YourResourceGroup --name YourAppServiceName --allowed-origins 'URL of the Application or * '

enter image description here

Now, check the CORS section under API.

You can see the Allowed Origins is added.

enter image description here

  • If you set CORS policy in both Code and in Azure Portal, then the setting of code will be overridden with the settings in Portal.

If the client App (App-#2) is hosted on (another) Azure App Service, where to find the external IP address which will be sent in Ajax calls to App-#1?

  • In this case we can use the hostname directly instead of IP Address like below
https://AppServiceName.azurewebsites.net/api/<controller>/<action

As mentioned in the GitHub use the below command to find the possibleOutboundIpAddresses .

enter image description here

Harshitha
  • 3,784
  • 2
  • 4
  • 9