ASP.NET Core MVC Intermediate

Deploying to Azure App Service

Azure App Service runs ASP.NET Core apps without managing servers. This lesson walks through creating an App Service and an Azure SQL database, publishing the app, setting configuration and connection strings safely, and the settings to check after the first deploy.

From your machine to Azure
  1. Startdotnet publishRelease build in a folder
  2. Step 2Zip deployUploaded to App Service
  3. Step 3App settingsEnvironment and connection strings
  4. Step 4Managed identitySigns in to Azure SQL without a password
  5. EndLive siteHTTPS, logs and Application Insights

What you need

An Azure subscription, the Azure CLI (or Visual Studio's Publish dialog), and an app that runs locally. App Service has a free F1 tier for trying things out; production sites usually start at a Basic or Premium tier for Always On, custom domains with TLS and deployment slots. Check current prices in the Azure pricing calculator before choosing.

Create and publish with the Azure CLI

Shell
az login
az group create --name shop-rg --location centralindia

az appservice plan create --name shop-plan --resource-group shop-rg --sku B1 --is-linux
az webapp create --name myshop-web --resource-group shop-rg --plan shop-plan --runtime "DOTNETCORE:9.0"

dotnet publish -c Release -o ./publish
cd publish && zip -r ../app.zip . && cd ..
az webapp deploy --name myshop-web --resource-group shop-rg --src-path app.zip --type zip

Configuration and the database

App settings in the portal become environment variables, overriding appsettings.json. Turn on a managed identity and let it into Azure SQL so the connection string needs no password.

Shell
az webapp config appsettings set -g shop-rg -n myshop-web --settings \
    ASPNETCORE_ENVIRONMENT=Production [email protected]

az webapp identity assign -g shop-rg -n myshop-web

az webapp config connection-string set -g shop-rg -n myshop-web -t SQLAzure --settings \
    Shop="Server=tcp:myshop-sql.database.windows.net;Database=Shop;Authentication=Active Directory Managed Identity;Encrypt=True"

# In the database (as the Entra admin), once:
#   CREATE USER [myshop-web] FROM EXTERNAL PROVIDER;
#   ALTER ROLE db_datareader ADD MEMBER [myshop-web];
#   ALTER ROLE db_datawriter ADD MEMBER [myshop-web];

After the first deploy

  • Turn on HTTPS Only and set the minimum TLS version to 1.2.
  • Turn on Always On (Basic tier and up) so background services and the first request are not slow.
  • Stream logs with az webapp log tail, and add Application Insights for errors and performance.
  • Apply EF Core migrations as a separate step (script or bundle), not at startup.
  • Use a staging deployment slot and swap, so users never see a half-deployed app.

Interview question

How do you give an App Service access to Azure SQL without storing a password?

Enable a system-assigned managed identity on the App Service, create a contained database user for it in Azure SQL with CREATE USER [app-name] FROM EXTERNAL PROVIDER and grant it roles, then use a connection string with Authentication=Active Directory Managed Identity (or Default). The app gets tokens from Azure automatically, so no secret is stored anywhere.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.