Skip to content

Commit 19207c3

Browse files
author
Jill Grant
authored
Merge pull request #286141 from v-albemi/app-service-17
Freshness - Azure App Service
2 parents f17bb20 + c1e2c26 commit 19207c3

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed
Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
11
---
2-
title: 'Tutorial: Host RESTful API with CORS'
3-
description: Learn how Azure App Service helps you host your RESTful APIs with CORS support. App Service can host both front-end web apps and back end APIs.
2+
title: 'Tutorial: Host a RESTful API with CORS'
3+
description: Learn how Azure App Service helps you host your RESTful APIs with CORS support. App Service can host both front-end web apps and back-end APIs.
44
ms.assetid: a820e400-06af-4852-8627-12b3db4a8e70
55
ms.devlang: csharp
66
ms.topic: tutorial
7-
ms.date: 01/31/2023
7+
ms.date: 09/05/2024
88
ms.custom: "UpdateFrequency3, devx-track-csharp, mvc, devcenter, devx-track-azurecli"
99
ms.author: msangapu
1010
author: msangapu-msft
1111
---
1212

1313
# Tutorial: Host a RESTful API with CORS in Azure App Service
1414

15-
[Azure App Service](overview.md) provides a highly scalable, self-patching web hosting service. In addition, App Service has built-in support for [Cross-Origin Resource Sharing (CORS)](https://wikipedia.org/wiki/Cross-Origin_Resource_Sharing) for RESTful APIs. This tutorial shows how to deploy an ASP.NET Core API app to App Service with CORS support. You configure the app using command-line tools and deploy the app using Git.
15+
[Azure App Service](overview.md) provides a highly scalable self-patching web hosting service. In addition, App Service has built-in support for [cross-origin resource sharing (CORS)](https://wikipedia.org/wiki/Cross-Origin_Resource_Sharing) for RESTful APIs. This tutorial shows how to deploy an ASP.NET Core API app to App Service with CORS support. You configure the app using command-line tools and deploy the app using Git.
1616

1717
In this tutorial, you learn how to:
1818

1919
> [!div class="checklist"]
20-
> * Create App Service resources using Azure CLI
21-
> * Deploy a RESTful API to Azure using Git
22-
> * Enable App Service CORS support
20+
> * Create App Service resources using Azure CLI.
21+
> * Deploy a RESTful API to Azure using Git.
22+
> * Enable App Service CORS support.
2323
24-
You can follow the steps in this tutorial on macOS, Linux, Windows.
24+
You can complete this tutorial on macOS, Linux, or Windows.
2525

2626
[!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)]
2727

2828
## Prerequisites
2929

30-
To complete this tutorial:
30+
* <a href="https://git-scm.com/" target="_blank">Install Git.</a>
31+
* <a href="https://dotnet.microsoft.com/download/dotnet-core/3.1" target="_blank">Install the latest .NET Core 3.1 SDK.</a>
3132

32-
* <a href="https://git-scm.com/" target="_blank">Install Git</a>
33-
* <a href="https://dotnet.microsoft.com/download/dotnet-core/3.1" target="_blank">Install the latest .NET Core 3.1 SDK</a>
34-
35-
## Create local ASP.NET Core app
33+
## Create a local ASP.NET Core app
3634

3735
In this step, you set up the local ASP.NET Core project. App Service supports the same workflow for APIs written in other languages.
3836

3937
### Clone the sample application
4038

41-
1. In the terminal window, `cd` to a working directory.
39+
1. In the terminal window, use `cd` to go to a working directory.
4240

43-
1. Clone the sample repository and change to the repository root.
41+
1. Clone the sample repository, and then go to the repository root.
4442

4543
```bash
4644
git clone https://github.com/Azure-Samples/dotnet-core-api
4745
cd dotnet-core-api
4846
```
4947

50-
This repository contains an app that's created based on the following tutorial: [ASP.NET Core Web API help pages using Swagger](/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio). It uses a Swagger generator to serve the [Swagger UI](https://swagger.io/swagger-ui/) and the Swagger JSON endpoint.
48+
This repository contains an app that's created based on the tutorial [ASP.NET Core web API documentation with Swagger / OpenAPI](/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio). It uses a Swagger generator to serve the [Swagger UI](https://swagger.io/swagger-ui/) and the Swagger JSON endpoint.
5149
5250
1. Make sure the default branch is `main`.
5351
@@ -56,7 +54,7 @@ In this step, you set up the local ASP.NET Core project. App Service supports th
5654
```
5755
5856
> [!TIP]
59-
> The branch name change isn't required by App Service. However, since many repositories are changing their default branch to `main` (see [Change deployment branch](deploy-local-git.md#change-deployment-branch)), this tutorial also shows you how to deploy a repository from `main`.
57+
> The branch name change isn't required by App Service. However, since many repositories are changing their default branch to `main` (see [Change deployment branch](deploy-local-git.md#change-deployment-branch)), this tutorial shows you how to deploy a repository from `main`.
6058

6159
### Run the application
6260

@@ -67,23 +65,23 @@ In this step, you set up the local ASP.NET Core project. App Service supports th
6765
dotnet run
6866
```
6967

70-
1. Navigate to `http://localhost:5000/swagger` in a browser to play with the Swagger UI.
68+
1. Navigate to `http://localhost:5000/swagger` in a browser to try the Swagger UI.
7169

72-
![ASP.NET Core API running locally](./media/app-service-web-tutorial-rest-api/azure-app-service-local-swagger-ui.png)
70+
![Screenshot of an ASP.NET Core API running locally.](./media/app-service-web-tutorial-rest-api/azure-app-service-local-swagger-ui.png)
7371

74-
1. Navigate to `http://localhost:5000/api/todo` and see a list of ToDo JSON items.
72+
1. Navigate to `http://localhost:5000/api/todo` to see a list of ToDo JSON items.
7573

76-
1. Navigate to `http://localhost:5000` and play with the browser app. Later, you will point the browser app to a remote API in App Service to test CORS functionality. Code for the browser app is found in the repository's _wwwroot_ directory.
74+
1. Navigate to `http://localhost:5000` and experiment with the browser app. Later, you'll point the browser app to a remote API in App Service to test CORS functionality. Code for the browser app is found in the repository's _wwwroot_ directory.
7775

78-
1. To stop ASP.NET Core at any time, press `Ctrl+C` in the terminal.
76+
1. To stop ASP.NET Core at any time, select **Ctrl+C** in the terminal.
7977

8078
[!INCLUDE [cloud-shell-try-it.md](~/reusable-content/ce-skilling/azure/includes/cloud-shell-try-it.md)]
8179

82-
## Deploy app to Azure
80+
## Deploy the app to Azure
8381

8482
In this step, you deploy your .NET Core application to App Service.
8583

86-
### Configure local git deployment
84+
### Configure local Git deployment
8785

8886
[!INCLUDE [Configure a deployment user](../../includes/configure-deployment-user-no-h.md)]
8987

@@ -103,7 +101,7 @@ In this step, you deploy your .NET Core application to App Service.
103101

104102
[!INCLUDE [app-service-plan-no-h](../../includes/app-service-web-git-push-to-azure-no-h.md)]
105103

106-
<pre>
104+
```
107105
Enumerating objects: 83, done.
108106
Counting objects: 100% (83/83), done.
109107
Delta compression using up to 8 threads
@@ -128,13 +126,13 @@ In this step, you deploy your .NET Core application to App Service.
128126
remote: Deployment successful.
129127
To https://&lt;app_name&gt;.scm.azurewebsites.net/&lt;app_name&gt;.git
130128
* [new branch] master -> master
131-
</pre>
129+
```
132130

133131
### Browse to the Azure app
134132

135-
1. Navigate to `http://<app_name>.azurewebsites.net/swagger` in a browser and play with the Swagger UI.
133+
1. Navigate to `http://<app_name>.azurewebsites.net/swagger` in a browser and view the Swagger UI.
136134

137-
![ASP.NET Core API running in Azure App Service](./media/app-service-web-tutorial-rest-api/azure-app-service-browse-app.png)
135+
![Screenshot of an ASP.NET Core API running in Azure App Service.](./media/app-service-web-tutorial-rest-api/azure-app-service-browse-app.png)
138136

139137
1. Navigate to `http://<app_name>.azurewebsites.net/swagger/v1/swagger.json` to see the _swagger.json_ for your deployed API.
140138

@@ -144,41 +142,41 @@ In this step, you deploy your .NET Core application to App Service.
144142

145143
Next, you enable the built-in CORS support in App Service for your API.
146144

147-
### Test CORS in sample app
145+
### Test CORS in the sample app
148146

149147
1. In your local repository, open _wwwroot/index.html_.
150148

151-
1. In Line 51, set the `apiEndpoint` variable to the URL of your deployed API (`http://<app_name>.azurewebsites.net`). Replace _\<appname>_ with your app name in App Service.
149+
1. On line 51, set the `apiEndpoint` variable to the URL of your deployed API (`http://<app_name>.azurewebsites.net`). Replace _\<appname>_ with your app name in App Service.
152150

153151
1. In your local terminal window, run the sample app again.
154152

155153
```bash
156154
dotnet run
157155
```
158156

159-
1. Navigate to the browser app at `http://localhost:5000`. Open the developer tools window in your browser (`Ctrl`+`Shift`+`i` in Chrome for Windows) and inspect the **Console** tab. You should now see the error message, `No 'Access-Control-Allow-Origin' header is present on the requested resource`.
157+
1. Navigate to the browser app at `http://localhost:5000`. Open the developer tools window in your browser (**Ctrl**+**Shift**+**i** in Chrome for Windows) and inspect the **Console** tab. You should now see the error message, `No 'Access-Control-Allow-Origin' header is present on the requested resource`.
160158

161-
![CORS error in browser client](./media/app-service-web-tutorial-rest-api/azure-app-service-cors-error.png)
159+
![Screenshot of the CORS error in the browser client.](./media/app-service-web-tutorial-rest-api/azure-app-service-cors-error.png)
162160

163-
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.
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, because the App Service app isn't sending the `Access-Control-Allow-Origin` header, the browser has prevented cross-domain content from loading.
164162
165-
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.
163+
In production, your browser app would have a public URL instead of the localhost URL, but the process for enabling CORS to a localhost URL is the same as the process for a public URL.
166164
167165
### Enable CORS
168166
169-
In the Cloud Shell, enable CORS to your client's URL by using the [`az webapp cors add`](/cli/azure/webapp/cors#az-webapp-cors-add) command. Replace the _&lt;app-name>_ placeholder.
167+
In Cloud Shell, enable CORS to your client's URL by using the [`az webapp cors add`](/cli/azure/webapp/cors#az-webapp-cors-add) command. Replace the _&lt;app-name>_ placeholder.
170168

171169
```azurecli-interactive
172170
az webapp cors add --resource-group myResourceGroup --name <app-name> --allowed-origins 'http://localhost:5000'
173171
```
174172

175-
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 '*'`.
173+
You can add multiple allowed origins by running the command multiple times or by adding a comma-separated list in `--allowed-origins`. To allow all origins, use `--allowed-origins '*'`.
176174

177175
### Test CORS again
178176

179177
Refresh the browser app at `http://localhost:5000`. The error message in the **Console** window is now gone, and you can see the data from the deployed API and interact with it. Your remote API now supports CORS to your browser app running locally.
180178

181-
![CORS success in browser client](./media/app-service-web-tutorial-rest-api/azure-app-service-cors-success.png)
179+
![Screenshot that shows CORS support in the browser client.](./media/app-service-web-tutorial-rest-api/azure-app-service-cors-success.png)
182180

183181
Congratulations, you're running an API in Azure App Service with CORS support.
184182
@@ -190,34 +188,34 @@ Congratulations, you're running an API in Azure App Service with CORS support.
190188
191189
#### App Service CORS vs. your CORS
192190
193-
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).
191+
You can use your own CORS utilities instead of App Service CORS for more flexibility. For example, you might want to specify different allowed origins for different routes or methods. Since App Service CORS lets you specify only one set of accepted origins for all API routes and methods, you would want to use your own CORS code. See how CORS is enabled in ASP.NET Core at [Enable CORS](/aspnet/core/security/cors).
194192
195-
The built-in App Service CORS feature does not have options to allow only specific HTTP methods or verbs for each origin that you specify. It will automatically allow all methods and headers for each origin defined. This behavior is similar to [ASP.NET Core CORS](/aspnet/core/security/cors) policies when you use the options `.AllowAnyHeader()` and `.AllowAnyMethod()` in the code.
193+
The built-in App Service CORS feature doesn't have options to allow only specific HTTP methods or verbs for each origin that you specify. It will automatically allow all methods and headers for each origin defined. This behavior is similar to [ASP.NET Core CORS](/aspnet/core/security/cors) policies when you use the options `.AllowAnyHeader()` and `.AllowAnyMethod()` in the code.
196194

197195
> [!NOTE]
198-
> Don't try to use App Service CORS and your own CORS code together. When used together, App Service CORS takes precedence and your own CORS code has no effect.
196+
> Don't try to use App Service CORS and your own CORS code together. If you try to use them together, App Service CORS takes precedence and your own CORS code has no effect.
199197
>
200198
>
201199
202200
#### How do I set allowed origins to a wildcard subdomain?
203201
204-
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:
202+
A wildcard subdomain like `*.contoso.com` is more restrictive than the wildcard origin `*`. 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 that by using Azure CLI, like so:
205203
206204
```azurecli-interactive
207205
az webapp cors add --resource-group <group-name> --name <app-name> --allowed-origins 'https://*.contoso.com'
208206
```
209207
210208
#### How do I enable the ACCESS-CONTROL-ALLOW-CREDENTIALS header on the response?
211209
212-
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`.
210+
If your app requires credentials such as cookies or authentication tokens to be sent, the browser might require the `ACCESS-CONTROL-ALLOW-CREDENTIALS` header on the response. To enable this in App Service, set `properties.cors.supportCredentials` to `true`:
213211
214212
```azurecli-interactive
215213
az resource update --name web --resource-group <group-name> \
216214
--namespace Microsoft.Web --resource-type config \
217215
--parent sites/<app-name> --set properties.cors.supportCredentials=true
218216
```
219217
220-
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).
218+
This operation isn't allowed when allowed origins include the wildcard origin `'*'`. Specifying `AllowAnyOrigin` and `AllowCredentials` isn't secure. Doing so 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).
221219
222220
[!INCLUDE [cli-samples-clean-up](../../includes/cli-samples-clean-up.md)]
223221
@@ -227,11 +225,11 @@ This operation is not allowed when allowed origins include the wildcard origin `
227225
What you learned:
228226
229227
> [!div class="checklist"]
230-
> * Create App Service resources using Azure CLI
231-
> * Deploy a RESTful API to Azure using Git
232-
> * Enable App Service CORS support
228+
> * Create App Service resources using Azure CLI.
229+
> * Deploy a RESTful API to Azure using Git.
230+
> * Enable App Service CORS support.
233231
234-
Advance to the next tutorial to learn how to authenticate and authorize users.
232+
Go to the next tutorial to learn how to authenticate and authorize users.
235233
236234
> [!div class="nextstepaction"]
237235
> [Tutorial: Authenticate and authorize users end-to-end](tutorial-auth-aad.md)
-843 Bytes
Loading

includes/app-service-web-git-push-to-azure-no-h.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ ms.custom: "include file"
2222
git remote add azure <deploymentLocalGitUrl-from-create-step>
2323
```
2424
25-
1. Push to the Azure remote to deploy your app with the following command. When Git Credential Manager prompts you for credentials, make sure you enter the credentials you created in **Configure a deployment user**, not the credentials you use to sign in to the Azure portal.
25+
1. Push to the Azure remote to deploy your app with the following command. When Git Credential Manager prompts you for credentials, make sure you enter the credentials you created in **Configure local git deployment**, not the credentials you use to sign in to the Azure portal.
2626
2727
```bash
2828
git push azure main
2929
```
3030
31-
This command may take a few minutes to run. While running, it displays information similar to the following example:
31+
This command might take a few minutes to run. While running, it displays information similar to the following example:

0 commit comments

Comments
 (0)