Martin Staufcik
Martin Staufcik

Reputation: 9490

Setting cors headers in Azure function

In my Azure function, I try to set cors headers:

var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = new ObjectContent<List<RecordEntity>>(data, new JsonMediaTypeFormatter(), "application/json");

response.Headers.Add("Access-Control-Allow-Origin", "*");
response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT");
response.Headers.Add("Access-Control-Allow-Headers", "*");
response.Headers.Add("Access-Control-Allow-Credentials", "true");

return response;

However, none of these headers get to the client in the http response, they are stripped from the response.

I suppose there is some other way of setting the cors headers, but cannot find how it works.

Upvotes: 2

Views: 5506

Answers (1)

Martin Staufcik
Martin Staufcik

Reputation: 9490

The CORS setting in the Azure portal is under Function App -> Platform Features.

What helped was to delete all Allowed origins from the list (as described in the link https://blogs.msdn.microsoft.com/benjaminperkins/2017/04/12/azure-functions-access-control-allow-credentials-with-cors/).

When the list of Allowed origins is empty, then finally the manually set cors headers are sent from Azure function in the http response to the client and are not stripped.

Upvotes: 3

Related Questions