Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS on API portion of Azure Static Web Apps #108

Closed
medhatelmasry opened this issue Jul 28, 2020 · 15 comments
Closed

CORS on API portion of Azure Static Web Apps #108

medhatelmasry opened this issue Jul 28, 2020 · 15 comments

Comments

@medhatelmasry
Copy link

My static web apps work quite well on Azure because both the API and the static app reside on the same endpoint. However, I realized that I cannot access the API on Azure Static Web Apps from another external app because of CORS. Please provide a mechanism so that CORS can be enabled on the API portion of a static web app on Azure.

@miwebst miwebst changed the title COS on API portion of Azure Static Web Apps CORS on API portion of Azure Static Web Apps Sep 15, 2020
@miwebst
Copy link
Contributor

miwebst commented Sep 15, 2020

Updating title slightly. So I believe you can do this today by setting the CORS headers in your Function response. Would that unblock your scenario?

@johnkors
Copy link

johnkors commented Nov 2, 2020

I have the same issue.

The API the static web app uses is not based on the Azure Functions based API from Azure Static Web apps, but an external existing ASP.NET Core API. Unfortunately, ASP.NET Core has no way to configure CORS settings to handle a dynamic environment/url like Azure Static Web apps (or Vercel, Netlify, Heroku Review apps for that matter) provides.

Tried to ping in the ASP.NET team here: dotnet/aspnetcore#27333

@anthonychu
Copy link
Member

@johnkors It doesn't look like having CORS support in the Static Web Apps function app would help you in this case. What may be useful to you is if there was a way to get a stable domain name for the staging slots #22 so that you can add a finite list of origins to the CORS middleware in your ASP.NET Core app.

@johnkors
Copy link

johnkors commented Nov 3, 2020

Yep, not using the functions, so obviously it won't help. Just saying, there will be scenarios where the APIs the Static Web Apps are connected to are not the bundled functions. And these APIs need to have CORS configuration that allow for dynamic origins, as they are created by Azure.

For my ASP.NET Core API (again, not a function), I got it working using a CorsOptions setting where I can dynamically validate origins (as opposed to static upfront pre-configured). See dotnet/aspnetcore#27435 (comment).

Setting up a stable domain name is easy for a stable URL, but not when it's subdomain changing for every PR received on GitHub.

@nogic1008
Copy link

Is it resolved by "Bring your own Functions"?

@Stuart88
Copy link

Yep. Could really do with being allowed to enable CORS for debugging against existing Azure Functions here.

So far I am really not having a very fun time with this Azure Static Web app malarkey. Highly limited and difficult to use...

@nogic1008
Copy link

It is resolved on #588 (comment)

@Reshmi-Sriram
Copy link
Member

Closing the issue, feel free to re-open if needed!

@johnkors
Copy link

Allow-Origin: * is not a solution . It's just bypassing the issue.

@thomasgauvin
Copy link
Contributor

@johnkors what is your specific use case? are you hosting your ASP.NET app on app service? If so, you could link your App Service to Azure Static Web Apps using the BYO API support https://docs.microsoft.com/en-us/azure/static-web-apps/apis-overview. This would alleviate your CORS issues

@johnkors
Copy link

johnkors commented Sep 7, 2022

Not hosting my backend at Azure. Heroku.

@thomasgauvin
Copy link
Contributor

thomasgauvin commented Sep 7, 2022

For your specific use-case @johnkors, it seems different than the initial issue, which requested their Static Web App APIs to be accessible on other sites. Your use-case seems to be the opposite, accessing another sites' API from your Static Web App, and it would require enabling CORS on your backend which you seem to have configured within your .NET API.

Is the remaining issue the varying URLs for preview environments?

@sheenamnarula
Copy link

Faced the same issue. This is because in static web apps , we don't have access to the deployed azure functions and headers are preserved in api. But a workaround can be using context.res.headers. Pass access-control-allow-origin headers in context.res.headers of api to be accessed outside.
For example : https://example.com has to access api of azure static web app, we can pass response like :

context.res["headers"] = {
'Access-Control-Allow-Origin': "https://example.com",
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS'
}

This will resolve the cors issue and api will be accessed outside static web app as well.

@johnkors
Copy link

johnkors commented Jan 3, 2023

Is the remaining issue the varying URLs for preview environments?

Yes.

@aluitink
Copy link

It is resolved on #588 (comment)

I'm using SWA with (managed) Azure Functions API - I cannot get the globalHeaders to work properly for CORS from API responses.

I found a *Note in the Global Headers documentation that indicates:

"Global headers do not affect API responses. Headers in API responses are preserved and returned to the client."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants