Skip to content

Commit 3e4173e

Browse files
Merge pull request #224382 from cephalin/patch-8
https://github.com/MicrosoftDocs/azure-docs/issues/102574
2 parents c9e1a45 + 5ebdd7c commit 3e4173e

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

articles/app-service/app-service-web-tutorial-rest-api.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how Azure App Service helps you host your RESTful APIs with C
44
ms.assetid: a820e400-06af-4852-8627-12b3db4a8e70
55
ms.devlang: csharp
66
ms.topic: tutorial
7-
ms.date: 04/28/2020
7+
ms.date: 01/31/2023
88
ms.custom: "devx-track-csharp, mvc, devcenter, seo-javascript-september2019, seo-javascript-october2019, seodec18, devx-track-azurecli"
99
---
1010

@@ -158,7 +158,7 @@ Next, you enable the built-in CORS support in App Service for your API.
158158
159159
![CORS error in browser client](./media/app-service-web-tutorial-rest-api/azure-app-service-cors-error.png)
160160
161-
Because of the domain mismatch between the browser app (`http://localhost:5000`) and remote resource (`http://<app_name>.azurewebsites.net`), and the fact that your API in App Service is not sending the `Access-Control-Allow-Origin` header, your browser has prevented cross-domain content from loading in your browser app.
161+
The domain mismatch between the browser app (`http://localhost:5000`) and remote resource (`http://<app_name>.azurewebsites.net`) is recognized by your browser as a cross-origin resource request. Also, the fact that your REST API the App Service app is not sending the `Access-Control-Allow-Origin` header, the browser has prevented cross-domain content from loading.
162162
163163
In production, your browser app would have a public URL instead of the localhost URL, but the way to enable CORS to a localhost URL is the same as a public URL.
164164
@@ -170,13 +170,7 @@ In the Cloud Shell, enable CORS to your client's URL by using the [`az webapp co
170170
az webapp cors add --resource-group myResourceGroup --name <app-name> --allowed-origins 'http://localhost:5000'
171171
```
172172

173-
You can set more than one client URL in `properties.cors.allowedOrigins` (`"['URL1','URL2',...]"`). You can also enable all client URLs with `"['*']"`.
174-
175-
> [!NOTE]
176-
> If your app requires credentials such as cookies or authentication tokens to be sent, the browser may require the `ACCESS-CONTROL-ALLOW-CREDENTIALS` header on the response. To enable this in App Service, set `properties.cors.supportCredentials` to `true` in your CORS config. This cannot be enabled when `allowedOrigins` includes `'*'`.
177-
178-
> [!NOTE]
179-
> Specifying `AllowAnyOrigin` and `AllowCredentials` is an insecure configuration and can result in cross-site request forgery. The CORS service returns an invalid CORS response when an app is configured with both methods.
173+
You can add multiple allowed origins by running the command multiple times or by adding a comma-separate list in `--allowed-origins`. To allow all origins, use `--allowed-origins '*'`.
180174

181175
### Test CORS again
182176

@@ -186,7 +180,13 @@ Refresh the browser app at `http://localhost:5000`. The error message in the **C
186180

187181
Congratulations, you're running an API in Azure App Service with CORS support.
188182
189-
## App Service CORS vs. your CORS
183+
## Frequently asked questions
184+
185+
- [App Service CORS vs. your CORS](#app-service-cors-vs-your-cors)
186+
- [How do I set allowed origins to a wildcard subdomain?](#how-do-i-set-allowed-origins-to-a-wildcard-subdomain)
187+
- [How do I enable the ACCESS-CONTROL-ALLOW-CREDENTIALS header on the response?](#how-do-i-enable-the-access-control-allow-credentials-header-on-the-response)
188+
189+
#### App Service CORS vs. your CORS
190190
191191
You can use your own CORS utilities instead of App Service CORS for more flexibility. For example, you may want to specify different allowed origins for different routes or methods. Since App Service CORS lets you specify one set of accepted origins for all API routes and methods, you would want to use your own CORS code. See how ASP.NET Core does it at [Enabling Cross-Origin Requests (CORS)](/aspnet/core/security/cors).
192192
@@ -197,6 +197,26 @@ The built-in App Service CORS feature does not have options to allow only specif
197197
>
198198
>
199199

200+
#### How do I set allowed origins to a wildcard subdomain?
201+
202+
A wildcard subdomain like `*.contoso.com` is more restrictive than the wildcard origin `*`. However, the app's CORS management page in the Azure portal doesn't let you set a wildcard subdomain as an allowed origin. However, you can do it using the Azure CLI, like so:
203+
204+
```azurecli-interactive
205+
az webapp cors add --resource-group <group-name> --name <app-name> --allowed-origins 'https://*.contoso.com'
206+
```
207+
208+
#### How do I enable the ACCESS-CONTROL-ALLOW-CREDENTIALS header on the response?
209+
210+
If your app requires credentials such as cookies or authentication tokens to be sent, the browser may require the `ACCESS-CONTROL-ALLOW-CREDENTIALS` header on the response. To enable this in App Service, set `properties.cors.supportCredentials` to `true`.
211+
212+
```azurecli-interactive
213+
az resource update --name web --resource-group <group-name> \
214+
--namespace Microsoft.Web --resource-type config \
215+
--parent sites/<app-name> --set properties.cors.supportCredentials=true
216+
```
217+
218+
This operation is not allowed when allowed origins include the wildcard origin `'*'`. Specifying `AllowAnyOrigin` and `AllowCredentials` is an insecure configuration and can result in cross-site request forgery. To allow credentials, try replacing the wildcard origin with [wildcard subdomains](#how-do-i-set-allowed-origins-to-a-wildcard-subdomain).
219+
200220
[!INCLUDE [cli-samples-clean-up](../../includes/cli-samples-clean-up.md)]
201221

202222
<a name="next"></a>

0 commit comments

Comments
 (0)