You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This article guides you through creating an Azure Policy that would enforce Microsoft Entra-only authentication when users create an Azure SQL Managed Instance, or a [logical server](logical-servers.md) for Azure SQL Database. To learn more about Microsoft Entra-only authentication during resource creation, see [Create server with Microsoft Entra-only authentication enabled in Azure SQL](authentication-azure-ad-only-authentication-create-server.md).
20
20
21
21
> [!NOTE]
22
-
> Although Azure Active Directory (Azure AD) has been [renamed to Microsoft Entra ID](/entra/fundamentals/new-name), Microsoft Entra-only and Azure AD-only authentication are used interchangeably in this article.
22
+
> Azure Active Directory (Azure AD) has been [renamed to Microsoft Entra ID](/entra/fundamentals/new-name).
23
23
24
24
In this article, you learn how to:
25
25
26
26
> [!div class="checklist"]
27
-
> - Create an Azure Policy that enforces logical server or managed instance creation with [Microsoft Entra-only authentication](authentication-azure-ad-only-authentication.md) enabled
27
+
> - Create an Azure Policy that enforces logical server or managed instance creation with [Microsoft Entra-only authentication with Azure SQL](authentication-azure-ad-only-authentication.md) enabled
28
28
> - Check Azure Policy compliance
29
29
30
30
## Prerequisite
@@ -40,41 +40,38 @@ Start off by creating an Azure Policy enforcing SQL Database or SQL Managed Inst
40
40
1. Under the Authoring settings, select **Definitions**.
41
41
1. In the **Search** box, search for *Microsoft Entra-only authentication*.
42
42
43
-
There are a few built-in policies available to enforce Microsoft Entra-only authentication. Look for the one available for your service:
44
-
45
-
- Azure SQL Database should have Microsoft Entra-only Authentication enabled
46
-
- Azure SQL Managed Instance should have Microsoft Entra-only Authentication enabled
43
+
There are a few built-in policies available to enforce Microsoft Entra-only authentication. Look for the one available for your service:
44
+
45
+
- Azure SQL Database should have Microsoft Entra-only Authentication enabled
46
+
- Azure SQL Managed Instance should have Microsoft Entra-only Authentication enabled
47
47
48
48
1. Select the policy name for your service. In this example, we'll use Azure SQL Database. Select **Azure SQL Database should have Microsoft Entra-only authentication enabled**.
49
49
1. Select **Assign policy** in the new menu.
50
50
51
-
> [!NOTE]
52
-
> The JSON script in the menu shows the built-in policy definition that can be used as a template to build a custom Azure Policy for SQL Database. The default is set to `Audit`.
53
-
54
-
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/assign-policy-azure-ad-only-authentication.png" alt-text="Screenshot of assigning Azure Policy for Microsoft Entra-only authentication." lightbox="media/authentication-azure-ad-only-authentication-policy-how-to/assign-policy-azure-ad-only-authentication.png":::
55
-
51
+
> [!NOTE]
52
+
> The JSON script in the menu shows the built-in policy definition that can be used as a template to build a custom Azure Policy for SQL Database. The default is set to `Audit`.
53
+
54
+
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/assign-policy-azure-ad-only-authentication.png" alt-text="Screenshot of assigning Azure Policy for Microsoft Entra-only authentication." lightbox="media/authentication-azure-ad-only-authentication-policy-how-to/assign-policy-azure-ad-only-authentication.png":::
55
+
56
56
1. In the **Basics** tab, add a **Scope** by using the selector (**...**) on the side of the box.
57
57
1. In the **Scope** pane, select your **Subscription** from the dropdown list menu, and select a **Resource Group** for this policy. Once you're done, use the **Select** button to save the selection.
58
58
59
-
> [!NOTE]
60
-
> If you don't select a resource group, the policy applies to the whole subscription.
61
-
62
-
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/adding-scope-policy-azure-ad-only-authentication.png" alt-text="Screenshot of adding Azure Policy scope for Microsoft Entra-only authentication.":::
63
-
59
+
> [!NOTE]
60
+
> If you don't select a resource group, the policy applies to the whole subscription.
61
+
62
+
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/adding-scope-policy-azure-ad-only-authentication.png" alt-text="Screenshot of adding Azure Policy scope for Microsoft Entra-only authentication.":::
63
+
64
64
1. Once you're back on the **Basics** tab, customize the **Assignment name** and provide an optional **Description**. Make sure the **Policy enforcement** is **Enabled**.
65
65
1. Go over to the **Parameters** tab. Unselect the option **Only show parameters that require input**.
66
66
1. Under **Effect**, select **Deny**. This setting prevents creating a logical server without Microsoft Entra-only authentication enabled.
67
67
68
-
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/deny-policy-azure-ad-only-authentication.png" alt-text="Screenshot of Azure Policy effect parameter for Microsoft Entra-only authentication." lightbox="media/authentication-azure-ad-only-authentication-policy-how-to/deny-policy-azure-ad-only-authentication.png":::
68
+
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/deny-policy-azure-ad-only-authentication.png" alt-text="Screenshot of Azure Policy effect parameter for Microsoft Entra-only authentication." lightbox="media/authentication-azure-ad-only-authentication-policy-how-to/deny-policy-azure-ad-only-authentication.png":::
69
69
70
70
1. In the **Non-compliance messages** tab, you can customize the policy message that displays if a violation of the policy has occurred. The message will let users know what policy was enforced during server creation.
71
71
72
-
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/non-compliance-message-policy-azure-ad-only-authentication.png" alt-text="Screenshot of Azure Policy noncompliance message for Microsoft Entra-only authentication." lightbox="media/authentication-azure-ad-only-authentication-policy-how-to/non-compliance-message-policy-azure-ad-only-authentication.png":::
73
-
74
-
1. Select **Review + create**. Review the policy and select the **Create** button.
75
-
76
-
> [!NOTE]
77
-
> It can take some time for the newly created policy to be enforced.
72
+
:::image type="content" source="media/authentication-azure-ad-only-authentication-policy-how-to/non-compliance-message-policy-azure-ad-only-authentication.png" alt-text="Screenshot of Azure Policy noncompliance message for Microsoft Entra-only authentication." lightbox="media/authentication-azure-ad-only-authentication-policy-how-to/non-compliance-message-policy-azure-ad-only-authentication.png":::
73
+
74
+
1. Select **Review + create**. Review the policy and select the **Create** button. It can take some time for the newly created policy to be enforced.
78
75
79
76
## Check policy compliance
80
77
@@ -86,8 +83,7 @@ Once the logical server is created with Microsoft Entra-only authentication, the
86
83
87
84
If the resource group that the policy was chosen to cover contains already created servers, the policy report will indicate those resources that are compliant and noncompliant.
88
85
89
-
> [!NOTE]
90
-
> Updating the compliance report can take some time. Changes related to resource creation or Microsoft Entra-only authentication settings aren't reported immediately.
86
+
Updating the compliance report can take some time. Changes related to resource creation or Microsoft Entra-only authentication settings aren't reported immediately.
# Connect to and query Azure SQL Database using .NET and Entity Framework Core
@@ -219,7 +219,7 @@ To update the database schema to match your data model using Entity Framework Co
219
219
220
220
---
221
221
222
-
3. A `Migrations` folder should appear in your project directory, along with a file called `InitialCreate` with unique numbers prepended. Run the migration to create the database using the following command:
222
+
3. A `Migrations` folder should appear in your project directory, along with a file called `InitialCreate` with unique numbers prepended. Run the migration to create the database using the following command, and the Entity Framework Core tooling creates the database schema in Azure defined by the `PersonDbContext` class.
223
223
224
224
## [Visual Studio](#tab/visual-studio)
225
225
@@ -235,8 +235,6 @@ To update the database schema to match your data model using Entity Framework Co
235
235
236
236
---
237
237
238
-
The Entity Framework Core tooling creates the database schema in Azure defined by the `PersonDbContext` class.
239
-
240
238
## Test the app locally
241
239
242
240
The app is ready to be tested locally. Make sure you're signed in to Visual Studio or the Azure CLI with the same account you set as the admin for your database.
@@ -247,9 +245,9 @@ The app is ready to be tested locally. Make sure you're signed in to Visual Stud
247
245
248
246
1. Modify the sample JSON to include values for the first name and family name. Select **Execute** to add a new record to the database. The API returns a successful response.
249
247
250
-
:::image type="content" source="media/passwordless-connections/api-testing-small.png" alt-text="Screenshot showing how to test the API." lightbox="media/passwordless-connections/api-testing.png":::
248
+
:::image type="content" source="media/azure-sql-dotnet-entity-framework-core-quickstart/api-testing-small.png" alt-text="Screenshot showing how to test the API." lightbox="media/azure-sql-dotnet-entity-framework-core-quickstart/api-testing.png":::
251
249
252
-
1. Expand the **GET** method on the Swagger UI page and select **Try it**. Select **Execute**, and the person you just created is returned.
250
+
1. Expand the `GET` method on the Swagger UI page and select **Try it**. Select **Execute**, and the person you just created is returned.
253
251
254
252
## Deploy to Azure App Service
255
253
@@ -261,13 +259,13 @@ The app is ready to be deployed to Azure. Visual Studio can create an Azure App
261
259
1. For the specific target, select **Azure App Service (Windows)**, and then select **Next**.
262
260
1. Select the green **+** icon to create a new App Service to deploy to and enter the following values:
263
261
264
-
-**Name**: Leave the default value.
265
-
-**Subscription name**: Select the subscription to deploy to.
266
-
-**Resource group**: Select **New** and create a new resource group called *msdocs-dotnet-sql*.
267
-
-**Hosting Plan**: Select **New** to open the hosting plan dialog. Leave the default values and select **OK**.
268
-
- Select **Create** to close the original dialog. Visual Studio creates the App Service resource in Azure.
269
-
270
-
:::image type="content" source="media/passwordless-connections/create-app-service-small.png" alt-text="Screenshot showing how to deploy with Visual Studio." lightbox="media/passwordless-connections/create-app-service.png":::
262
+
-**Name**: Leave the default value.
263
+
-**Subscription name**: Select the subscription to deploy to.
264
+
-**Resource group**: Select **New** and create a new resource group called *msdocs-dotnet-sql*.
265
+
-**Hosting Plan**: Select **New** to open the hosting plan dialog. Leave the default values and select **OK**.
266
+
- Select **Create** to close the original dialog. Visual Studio creates the App Service resource in Azure.
267
+
268
+
:::image type="content" source="media/azure-sql-dotnet-entity-framework-core-quickstart/create-app-service-small.png" alt-text="Screenshot showing how to deploy with Visual Studio." lightbox="media/azure-sql-dotnet-entity-framework-core-quickstart/create-app-service.png":::
271
269
272
270
1. Once the resource is created, make sure you select in the list of app services, and then select **Next**.
273
271
@@ -341,7 +339,7 @@ The Azure portal allows you to work with managed identities and run queries agai
341
339
GO
342
340
```
343
341
344
-
:::image type="content" source="media/passwordless-connections/query-editor-small.png" alt-text="Screenshot showing how to use the Azure Query editor." lightbox="media/passwordless-connections/query-editor.png":::
342
+
:::image type="content" source="media/azure-sql-dotnet-entity-framework-core-quickstart/query-editor-small.png" alt-text="Screenshot showing how to use the Azure Query editor." lightbox="media/azure-sql-dotnet-entity-framework-core-quickstart/query-editor.png":::
345
343
346
344
This SQL script creates a SQL database user that maps back to the managed identity of your App Service instance. It also assigns the necessary SQL roles to the user to allow your app to read, write, and modify the data and schema of your database. After this step is completed, your services are connected.
Copy file name to clipboardExpand all lines: azure-sql/database/azure-sql-dotnet-quickstart.md
+20-23Lines changed: 20 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
---
2
-
title: Connect to and query Azure SQL Database using .NET and the Microsoft.Data.SqlClient library
2
+
title: "Connect to and Query Azure SQL Database Using .NET and the Microsoft.Data.SqlClient Library"
3
3
description: Learn how to connect to a database in Azure SQL Database and query data using .NET
4
4
author: WilliamDAssafMSFT
5
5
ms.author: wiassaf
6
6
ms.reviewer: alexwolf, mathoma, vanto
7
-
ms.date: 09/17/2024
7
+
ms.date: 08/07/2025
8
8
ms.service: azure-sql-database
9
9
ms.subservice: security
10
10
ms.topic: quickstart
11
-
monikerRange: "=azuresql || =azuresql-db"
12
11
ms.custom:
13
12
- passwordless-dotnet
14
13
- sfi-ropc-nochange
14
+
monikerRange: "=azuresql || =azuresql-db"
15
15
---
16
16
17
17
# Connect to and query Azure SQL Database using .NET and the Microsoft.Data.SqlClient library
@@ -123,8 +123,8 @@ Replace the contents of the `Program.cs` file with the following code, which per
123
123
124
124
- Retrieves the passwordless connection string from `appsettings.json`
125
125
- Creates a `Persons` table in the database during startup (for testing scenarios only)
126
-
- Creates an HTTP GET endpoint to retrieve all records stored in the `Persons` table
127
-
- Creates an HTTP POST endpoint to add new records to the `Persons` table
126
+
- Creates an `HTTP GET` endpoint to retrieve all records stored in the `Persons` table
127
+
- Creates an `HTTP` POST endpoint to add new records to the `Persons` table
128
128
129
129
```csharp
130
130
usingMicrosoft.Data.SqlClient;
@@ -226,9 +226,9 @@ The app is ready to be tested locally. Make sure you're signed in to Visual Stud
226
226
227
227
1) Modify the sample JSON to include values for the `first` and `last` name. Select **Execute** to add a new record to the database. The API returns a successful response.
228
228
229
-
:::image type="content" source="media/passwordless-connections/api-testing-small.png" alt-text="Screenshot showing how to test the API." lightbox="media/passwordless-connections/api-testing.png":::
229
+
:::image type="content" source="media/azure-sql-dotnet-quickstart/api-testing-small.png" alt-text="Screenshot showing how to test the API." lightbox="media/azure-sql-dotnet-quickstart/api-testing.png":::
230
230
231
-
1) Expand the **GET** method on the Swagger UI page and select **Try it**. Choose **Execute**, and the person you just created is returned.
231
+
1) Expand the `GET` method on the Swagger UI page and select **Try it**. Choose **Execute**, and the person you just created is returned.
232
232
233
233
## Deploy to Azure App Service
234
234
@@ -240,18 +240,17 @@ The app is ready to be deployed to Azure. Visual Studio can create an Azure App
240
240
1. For the specific target, select **Azure App Service (Windows)**, and then select **Next**.
241
241
1. Select the **+** icon to create a new App Service to deploy to and enter the following values:
242
242
243
-
***Name**: Leave the default value.
244
-
***Subscription name**: Select the subscription to deploy to.
245
-
***Resource group**: Select **New** and create a new resource group called *msdocs-dotnet-sql*.
246
-
***Hosting Plan**: Select **New** to open the hosting plan dialog. Leave the default values and select **OK**.
247
-
* Select **Create** to close the original dialog. Visual Studio creates the App Service resource in Azure.
243
+
-**Name**: Leave the default value.
244
+
-**Subscription name**: Select the subscription to deploy to.
245
+
-**Resource group**: Select **New** and create a new resource group called *msdocs-dotnet-sql*.
246
+
-**Hosting Plan**: Select **New** to open the hosting plan dialog. Leave the default values and select **OK**.
247
+
- Select **Create** to close the original dialog. Visual Studio creates the App Service resource in Azure.
248
248
249
-
:::image type="content" source="media/passwordless-connections/create-app-service-small.png" alt-text="Screenshot showing how to deploy with Visual Studio." lightbox="media/passwordless-connections/create-app-service.png":::
249
+
:::image type="content" source="media/azure-sql-dotnet-quickstart/create-app-service-small.png" alt-text="Screenshot showing how to deploy with Visual Studio." lightbox="media/azure-sql-dotnet-quickstart/create-app-service.png":::
250
250
251
251
1. Once the resource is created, make sure it's selected in the list of app services, and then select **Next**.
252
252
1. On the **API Management** step, select the **Skip this step** checkbox at the bottom and then choose **Finish**.
253
253
1. On the Finish step, select **Close** if the dialog doesn't close automatically.
254
-
255
254
1. Select **Publish** in the upper right of the publishing profile summary to deploy the app to Azure.
256
255
257
256
When the deployment finishes, Visual Studio launches the browser to display the hosted app, but at this point the app doesn't work correctly on Azure. You still need to configure the secure connection between the App Service and the SQL database to retrieve your data.
@@ -270,19 +269,17 @@ When the deployment finishes, Visual Studio launches the browser to display the
270
269
271
270
## Test the deployed application
272
271
273
-
1) Select the **Browse** button at the top of App Service overview page to launch the root url of your app.
274
-
275
-
2) Append the `/swagger/index.html` path to the URL to load the same Swagger test page you used locally.
276
-
277
-
3) Execute test GET and POST requests to verify that the endpoints work as expected.
278
-
279
-
> [!TIP]
280
-
> If you receive a 500 Internal Server error while testing, it might be due to your database networking configurations. Verify that your logical server is configured with the settings outlined in the [Configure the database](/azure/azure-sql/database/azure-sql-dotnet-quickstart#configure-the-database) section.
272
+
1. Select the **Browse** button at the top of App Service overview page to launch the root url of your app.
273
+
1. Append the `/swagger/index.html` path to the URL to load the same Swagger test page you used locally.
274
+
1. Execute test `GET` and POST requests to verify that the endpoints work as expected.
281
275
276
+
> [!TIP]
277
+
> If you receive a 500 Internal Server error while testing, it might be due to your database networking configurations. Verify that your logical server is configured with the settings outlined in the [Configure the database](/azure/azure-sql/database/azure-sql-dotnet-quickstart#configure-the-database) section.
278
+
282
279
Your application is now connected to Azure SQL Database in both local and hosted environments.
0 commit comments