45

I'm testing an Azure Functions app locally with the Azure Functions CLI tooling. Is there any way I can configure CORS settings for the local host?

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Does a local instance not expose the same settings UI documented at https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#manage-cors ? – sideshowbarker Feb 21 '17 at 23:01
  • no, you just host the runtime, not the portal – Mark Heath Feb 22 '17 at 08:39
  • I’m completely unfamiliar with the architecture but you know that the only place where cross-origin/CORS restrictions are enforced is by browsers, on web apps running in browsers, right? So if this runtime that apps are executing in is outside the browser, then I’d assume nothing is preventing apps that run in it from making programmatic requests for any web URLs you want them to. So I guess I’m confused about what’s not working and what specific error you might be running into. – sideshowbarker Feb 22 '17 at 12:26
  • yes, my use case is a little unusual. it's running some JavaScript tests from the browser that automate calling my API. So the browser is calling out to localhost, but failing because of CORS. – Mark Heath Feb 22 '17 at 13:00
  • Ah OK, there must be some config file for the web server on localhost? Or some source for it you can edit and then rebuild it from? Or is it just a binary that you don’t have the source for? Regardless, at the simplest level if you can access the config or source for it to make it send an `Access-Control-Allow-Origin: *` response header, then the browser will allow your JavaScript tests to work as expected. (Now also curious to know that web server it actually runs on localhost… IIS?) – sideshowbarker Feb 22 '17 at 13:09
  • it's running this: https://github.com/Azure/azure-webjobs-sdk-script/tree/dev/src/WebJobs.Script.WebHost – Mark Heath Feb 22 '17 at 13:49

4 Answers4

94

You can configure CORS in the local settings file local.settings.json:

{
  "Values": {
  },
  "Host": {
    "CORS": "*"
  }
}

Settings in the local.settings.json file are used only when you're running projects locally

Leonardo
  • 2,065
  • 2
  • 26
  • 27
  • 6
    If you want to allow only a single domain it is important to not have a `/` at the end. So `http://localhost:4200/` does not work, but `http://localhost:4200` works as value of `CORS`. – sschoof Apr 14 '20 at 06:56
  • Note that "Host" goes OUTSIDE of "Values". I tried to nest it inside, but this breaks things. – icanfathom Sep 11 '22 at 13:58
  • If you are passing authorization token then also include `CORSCredentials: true` . Example ```"Host": { "CORS": "https://localhost:3001", "CORSCredentials": true }``` – Shweta patel Mar 17 '23 at 15:35
37

You can start the host like this

func host start --cors *

You can also be more specific and provide a comma delimited list of allowed urls

More here: https://github.com/Azure/azure-webjobs-sdk-script/issues/1012

ayls
  • 2,401
  • 2
  • 19
  • 24
  • 26
    For me adding `, "Host": { "LocalHttpPort": 7071, "CORS": "*" }` to `local.settings.json` was easier (suggested by someone in a deleted answer to this Q) – Martin Smith Jul 24 '18 at 19:29
  • 1
    Yup, changing `local.settings.json` per the above fixed the problem for me (Windows 8.1, VS Code). Anybody knows why this has to be in `local.settings.json`? I already had the same setting in `host.json`, but that did not seem to work when developing locally. – ebhh2001 Sep 13 '18 at 04:29
4

Another easy way to configure CORS on Azure Functions is to use Azure Portal,

1- Go to the Function App Settings in Azure Portal

enter image description here

2 - Click on CORS and add your local host url

enter image description here

And there you have it!!

Hope this helps someone.

Saif Asad
  • 779
  • 10
  • 8
  • 3
    The questions is specific for local development, not the one on Azure. – Miguel Angel Dec 20 '18 at 15:12
  • 3
    Is there a way to configure this in the code and not to have to do it via the azure web portal? – Philip Claren Feb 01 '19 at 13:59
  • This seems insecure. You are telling the cloud to accept localhost from any computer out there. Hopefully you have other security in place as well to prevent that. Rather you would want to allow this on your local machine only. – Dirk R Aug 02 '23 at 11:59
2

If you're having issues passing in the params via Visual Studio's Debug's "Application Arguments". This is how to pass the params from the command line:

1) Open an ordinary command prompt.

2) cd to your solution's compiled dll, i.e. "Your Solution Path"\bin\Debug\netstandard2.0

3) start the Azure function runtime from the command line, i.e.:

dotnet "C:\Users\USER\AppData\Local\Azure.Functions.V2.Cli\func.dll" host start --port 7071 --cors * --pause-on-error 

4) To debug in Visual Studio, Debug->Attach to Process.. and attach to the donet.exe that will be running.

Hope that helps prevent someone from banging their head too much...

Mark
  • 21
  • 1