Skip to content

Commit 4c24719

Browse files
committed
Update to call out User-assigned Managed Identity
Update to call out User-assigned Managed Identity scenarios explicitly
1 parent 3bb8727 commit 4c24719

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

articles/app-service/includes/tutorial-connect-msi-azure-database/code-sql-mi.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: xfz11
33
ms.service: service-connector
44
ms.topic: include
5-
ms.date: 10/26/2023
5+
ms.date: 04/17/2024
66
ms.author: xiaofanzhou
77
---
88

@@ -18,6 +18,10 @@ ms.author: xiaofanzhou
1818

1919
```csharp
2020
using Microsoft.Data.SqlClient;
21+
22+
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
23+
// For system-assigned managed identity:"Server=tcp:<server-name>.database.windows.net;Database=<database-name>;Authentication=Active Directory Default;TrustServerCertificate=True"
24+
// For user-assigned managed identity: "Server=tcp:<server-name>.database.windows.net;Database=<database-name>;Authentication=Active Directory Default;User Id=<client-id-of-user-assigned-identity>;TrustServerCertificate=True"
2125
2226
string connectionString =
2327
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
@@ -77,7 +81,7 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
7781
python -m pip install pyodbc
7882
```
7983
80-
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
84+
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. Uncomment the part of the code snippet for the authentication type you want to use.
8185
```python
8286
import os;
8387
import pyodbc
@@ -105,7 +109,7 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
105109
```bash
106110
npm install mssql
107111
```
108-
1. Get the Azure SQL Database connection configurations from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
112+
1. Get the Azure SQL Database connection configurations from the environment variables added by Service Connector. Uncomment the part of the code snippet for the authentication type you want to use.
109113
```javascript
110114
import sql from 'mssql';
111115

articles/app-service/tutorial-connect-msi-sql-database.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: cephalin
66

77
ms.devlang: csharp
88
ms.topic: tutorial
9-
ms.date: 04/01/2023
9+
ms.date: 04/17/2024
1010
ms.custom: devx-track-csharp, mvc, cli-validate, devx-track-azurecli, devx-track-dotnet, AppServiceConnectivity
1111
---
1212
# Tutorial: Connect to SQL Database from .NET App Service without secrets using a managed identity
@@ -158,8 +158,7 @@ The steps you follow for your project depends on whether you're using [Entity Fr
158158
```
159159
160160
> [!NOTE]
161-
> The [Active Directory Default](/sql/connect/ado-net/sql/azure-active-directory-authentication#using-active-directory-default-authentication) authentication type can be used both on your local machine and in Azure App Service. The driver attempts to acquire a token from Microsoft Entra ID using various means. If the app is deployed, it gets a token from the app's managed identity. If the app is running locally, it tries to get a token from Visual Studio, Visual Studio Code, and Azure CLI.
162-
>
161+
> The [Active Directory Default](/sql/connect/ado-net/sql/azure-active-directory-authentication#using-active-directory-default-authentication) authentication type can be used both on your local machine and in Azure App Service. The driver attempts to acquire a token from Microsoft Entra ID using various means. If the app is deployed, it gets a token from the app's system-assigned managed identity. It can also authenticate with a user-assigned managed identity if you include: `User Id=<client-id-of-user-assigned-managed-identity>;` in your connection string. If the app is running locally, it tries to get a token from Visual Studio, Visual Studio Code, and Azure CLI.
163162
164163
That's everything you need to connect to SQL Database. When you debug in Visual Studio, your code uses the Microsoft Entra user you configured in [2. Set up your dev environment](#2-set-up-your-dev-environment). You'll set up SQL Database later to allow connection from the managed identity of your App Service app. The `DefaultAzureCredential` class caches the token in memory and retrieves it from Microsoft Entra ID just before expiration. You don't need any custom code to refresh the token.
165164
@@ -176,13 +175,23 @@ The steps you follow for your project depends on whether you're using [Entity Fr
176175
1. In your DbContext object (in *Models/MyDbContext.cs*), add the following code to the default constructor.
177176
178177
```csharp
178+
Azure.Identity.DefaultAzureCredential credential;
179+
var managedIdentityClientId = ConfigurationManager.AppSettings["ManagedIdentityClientId"];
180+
if(managedIdentityClientId != null ) {
181+
//User-assigned managed identity Client ID is passed in via ManagedIdentityClientId
182+
var defaultCredentialOptions = new DefaultAzureCredentialOptions { ManagedIdentityClientId = managedIdentityClientId };
183+
credential = new Azure.Identity.DefaultAzureCredential(defaultCredentialOptions);
184+
}
185+
else {
186+
//System-assigned managed identity or logged-in identity of Visual Studio, Visual Studio Code, Azure CLI or Azure PowerShell
187+
credential = new Azure.Identity.DefaultAzureCredential();
188+
}
179189
var conn = (System.Data.SqlClient.SqlConnection)Database.Connection;
180-
var credential = new Azure.Identity.DefaultAzureCredential();
181190
var token = credential.GetToken(new Azure.Core.TokenRequestContext(new[] { "https://database.windows.net/.default" }));
182191
conn.AccessToken = token.Token;
183192
```
184193
185-
This code uses [Azure.Identity.DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) to get a useable token for SQL Database from Microsoft Entra ID and then adds it to the database connection. While you can customize `DefaultAzureCredential`, by default it's already versatile. When it runs in App Service, it uses app's system-assigned managed identity. When it runs locally, it can get a token using the logged-in identity of Visual Studio, Visual Studio Code, Azure CLI, and Azure PowerShell.
194+
This code uses [Azure.Identity.DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) to get a useable token for SQL Database from Microsoft Entra ID and then adds it to the database connection. While you can customize `DefaultAzureCredential`, by default it's already versatile. When it runs in App Service, it uses the app's system-assigned managed identity by default. If you prefer to use a user-assigned managed identity, add a new App setting named `ManagedIdentityClientId` and enter the `Client Id` GUID from your user-assigned managed identity in the `value` field. When it runs locally, it can get a token using the logged-in identity of Visual Studio, Visual Studio Code, Azure CLI, and Azure PowerShell.
186195
187196
1. In *Web.config*, find the connection string called `MyDbConnection` and replace its `connectionString` value with `"server=tcp:<server-name>.database.windows.net;database=<db-name>;"`. Replace _\<server-name>_ and _\<db-name>_ with your server name and database name. This connection string is used by the default constructor in *Models/MyDbContext.cs*.
188197

0 commit comments

Comments
 (0)