Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit bbf85ae

Browse files
authored
Refresh articles (#35370)
* Refresh articles * edit
1 parent 260e9f2 commit bbf85ae

6 files changed

Lines changed: 442 additions & 439 deletions

azure-sql/database/authentication-aad-service-principal-tutorial.md

Lines changed: 105 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Create Microsoft Entra users using service principals
2+
title: Create Microsoft Entra Users Using Service Principals
33
description: This tutorial walks you through creating Microsoft Entra users with a Microsoft Entra application (service principal) in Azure SQL Database.
44
author: VanMSFT
55
ms.author: vanto
6-
ms.reviewer: wiassaf, vanto, mathoma
7-
ms.date: 02/16/2024
6+
ms.reviewer: wiassaf, mathoma
7+
ms.date: 09/18/2025
88
ms.service: azure-sql-database
99
ms.subservice: security
1010
ms.topic: tutorial
@@ -17,7 +17,7 @@ ms.custom:
1717

1818
# Tutorial: Create Microsoft Entra users using Microsoft Entra applications
1919

20-
[!INCLUDE[appliesto-sqldb](../includes/appliesto-sqldb.md)]
20+
[!INCLUDE [appliesto-sqldb](../includes/appliesto-sqldb.md)]
2121

2222
This article explains how to configure a service principal so it can create Microsoft Entra users in Azure SQL Database. This capability enables programmatic configuration of access management to Azure SQL resources for users and applications in your Microsoft Entra tenant.
2323

@@ -38,54 +38,54 @@ In this tutorial, you learn how to:
3838

3939
- An existing [Azure SQL Database](single-database-create-quickstart.md) deployment. We assume you have a working SQL Database for this tutorial.
4040
- Microsoft Entra `Privileged Role Administrator` permissions in the tenant where your SQL database resides.
41-
- The latest version of the [Az.Sql](https://www.powershellgallery.com/packages/Az.Sql/) PowerShell module.
41+
- The latest version of the [Az.Sql](https://www.powershellgallery.com/packages/Az.Sql/) PowerShell module.
4242
- The latest version of the [Microsoft.Graph](https://www.powershellgallery.com/packages/Microsoft.Graph) PowerShell module.
4343

4444
## Assign an identity to the logical server
4545

4646
1. Connect to Azure, specifying the Microsoft Entra tenant that hosts your SQL database. The Tenant ID can be found on the **Overview** page for your **Microsoft Entra ID** resource in the [Azure portal](https://portal.azure.com). Copy the **Tenant ID** and then run the following PowerShell command:
4747

48-
- Replace `<TenantId>` with your **Tenant ID**.
48+
- Replace `<TenantId>` with your **Tenant ID**.
4949

50-
```powershell
51-
Connect-AzAccount -Tenant <TenantId>
52-
```
50+
```powershell
51+
Connect-AzAccount -Tenant <TenantId>
52+
```
5353

54-
Record the `TenantId` for future use in this tutorial.
54+
Record the `TenantId` for future use in this tutorial.
5555

5656
1. Generate a system-assigned managed identity and assign it to the [logical server in Azure](logical-servers.md). Execute the following PowerShell command:
5757

58-
- Replace `<ResourceGroupName>` and `<ServerName>` with your resources in the [Set-AzSqlServer](/powershell/module/az.sql/set-azsqlserver) command. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
58+
- Replace `<ResourceGroupName>` and `<ServerName>` with your resources in the [Set-AzSqlServer](/powershell/module/az.sql/set-azsqlserver) command. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
5959

60-
```powershell
61-
Set-AzSqlServer -ResourceGroupName <ResourceGroupName> -ServerName <ServerName> -AssignIdentity
62-
```
60+
```powershell
61+
Set-AzSqlServer -ResourceGroupName <ResourceGroupName> -ServerName <ServerName> -AssignIdentity
62+
```
6363

6464
1. Check the server identity was successfully assigned. Execute the following PowerShell command:
6565

66-
- Replace `<ResourceGroupName>` and `<ServerName>` with your resources. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
67-
68-
```powershell
69-
$xyz = Get-AzSqlServer -ResourceGroupName <ResourceGroupName> -ServerName <ServerName>
70-
$xyz.identity
71-
```
66+
- Replace `<ResourceGroupName>` and `<ServerName>` with your resources. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
7267

73-
Your output should show you `PrincipalId`, `Type`, and `TenantId`. The identity assigned is the `PrincipalId`.
68+
```powershell
69+
$xyz = Get-AzSqlServer -ResourceGroupName <ResourceGroupName> -ServerName <ServerName>
70+
$xyz.identity
71+
```
72+
73+
Your output should show you `PrincipalId`, `Type`, and `TenantId`. The identity assigned is the `PrincipalId`.
7474

7575
1. You can also check the identity by going to the [Azure portal](https://portal.azure.com).
7676

77-
- In the **Microsoft Entra ID** resource, go to **Enterprise applications**. Type in the name of your logical server. The **Object ID** that appears on the resource is the ID of the primary server identity.
77+
- In the **Microsoft Entra ID** resource, go to **Enterprise applications**. Type in the name of your logical server. The **Object ID** that appears on the resource is the ID of the primary server identity.
7878

79-
:::image type="content" source="media/authentication-aad-service-principals-tutorial/enterprise-applications-object-id.png" alt-text="Screenshot shows where to find the Object ID for an enterprise application.":::
79+
:::image type="content" source="media/authentication-aad-service-principals-tutorial/enterprise-applications-object-id.png" alt-text="Screenshot shows where to find the Object ID for an enterprise application.":::
8080

8181
## Add server identity to Directory Readers role
8282

83-
The server identity requires permissions to query Microsoft Entra ID for administrative functions, which includes creating Microsoft Entra users and logins, and doing group expansion to apply user permissions based on their Microsoft Entra group membership. If server identity permissions to query Microsoft Entra ID are revoked, or the server identity is deleted, Microsoft Entra authentication stops working.
83+
The server identity requires permissions to query Microsoft Entra ID for administrative functions, which includes creating Microsoft Entra users and logins, and doing group expansion to apply user permissions based on their Microsoft Entra group membership. If server identity permissions to query Microsoft Entra ID are revoked, or the server identity is deleted, Microsoft Entra authentication stops working.
8484

85-
Assign Microsoft Entra query permissions to the server identity by adding it to the [Directory Readers](/entra/identity/role-based-access-control/permissions-reference#directory-readers) role or assigning the following lower-level [Microsoft Graph](/graph/overview) permissions:
85+
Assign Microsoft Entra query permissions to the server identity by adding it to the [Directory Readers](/entra/identity/role-based-access-control/permissions-reference#directory-readers) role or assigning the following lower-level [Microsoft Graph](/graph/overview) permissions:
8686
- [User.Read.All](/graph/permissions-reference#userreadall)
87-
- [GroupMember.Read.All](/graph/permissions-reference#groupmemberreadall), and
88-
- [Application.Read.All](/graph/permissions-reference#applicationreadall).
87+
- [GroupMember.Read.All](/graph/permissions-reference#groupmemberreadall), and
88+
- [Application.Read.All](/graph/permissions-reference#applicationreadall).
8989

9090
> [!NOTE]
9191
> This script must be executed by a Microsoft Entra `Privileged Role Administrator` or higher role.
@@ -146,13 +146,13 @@ if ($isDirReader -eq $null) {
146146
```
147147

148148
> [!NOTE]
149-
> The output from this script indicates if the identity is assigned to the **Directory Readers** role. You can re-run the script if you are unsure if the permission was granted.
149+
> The output from this script indicates if the identity is assigned to the **Directory Readers** role. You can rerun the script if you're unsure if the permission was granted.
150150
151151
For a similar approach on how to assign the **Directory Readers** role for SQL Managed Instance, see [Set Microsoft Entra admin](authentication-aad-configure.md#azure-sql-managed-instance).
152152

153153
In production environments, a common management practice is to assign the **Directory Readers** role to a role-assignable group in Microsoft Entra ID. Then, group owners can add managed identities to the group. This maintains the principle of least privilege, and bypasses the need for a **Privileged Role Administrator** to grant the **Directory Readers** role individually to every SQL instance. For more information on this feature, see [Directory Readers role in Microsoft Entra ID for Azure SQL](authentication-aad-directory-readers-role.md).
154154

155-
<a name='create-a-service-principal-an-azure-ad-application-in-azure-ad'></a>
155+
<a id="create-a-service-principal-an-azure-ad-application-in-azure-ad"></a>
156156

157157
## Create an application in Microsoft Entra ID
158158

@@ -164,11 +164,11 @@ To register your applications:
164164

165165
1. In the Azure portal, select **Microsoft Entra ID** > **App registrations** > **New registration**.
166166

167-
:::image type="content" source="media/authentication-aad-service-principals-tutorial/new-registration.png" alt-text="Screenshot shows the Register an application page.":::
167+
:::image type="content" source="media/authentication-aad-service-principals-tutorial/new-registration.png" alt-text="Screenshot shows the Register an application page.":::
168168

169-
After the app registration is created, the **Application (client) ID** value is generated and displayed. Record this value for future use in this tutorial.
169+
After the app registration is created, the **Application (client) ID** value is generated and displayed. Record this value for future use in this tutorial.
170170

171-
![Screenshot of the Azure portal that shows the App ID.](./media/active-directory-interactive-connect-azure-sql-db/image2.png)
171+
![Screenshot of the Azure portal that shows the App ID.](./media/active-directory-interactive-connect-azure-sql-db/image2.png)
172172

173173
1. Create a client secret for the application to sign in with. Follow [upload a certificate or create a secret for signing in](/entra/identity-platform/howto-create-service-principal-portal#set-up-authentication). Record the client secret for *DBOwnerApp* for future use in this tutorial.
174174

@@ -185,12 +185,12 @@ Connect to your SQL Database using a Microsoft Entra identity that has permissio
185185
186186
1. Create the user *DBOwnerApp* in the SQL Database using the following T-SQL command:
187187

188-
```sql
189-
CREATE USER [DBOwnerApp] FROM EXTERNAL PROVIDER
190-
GO
191-
```
188+
```sql
189+
CREATE USER [DBOwnerApp] FROM EXTERNAL PROVIDER
190+
GO
191+
```
192192

193-
2. In order to create other Microsoft Entra users, at minimum, the `ALTER ANY USER` SQL permission is required. This permission is also inherited through membership in `db_owner`, and through assignment as the Microsoft Entra admin. The following examples demonstrate three different options to assign permissions to *DBOwnerApp* that allow it to create other Microsoft Entra users in the database.
193+
1. In order to create other Microsoft Entra users, at minimum, the `ALTER ANY USER` SQL permission is required. This permission is also inherited through membership in `db_owner`, and through assignment as the Microsoft Entra admin. The following examples demonstrate three different options to assign permissions to *DBOwnerApp* that allow it to create other Microsoft Entra users in the database.
194194

195195
You can add *DBOwnerApp* to the `db_owner` role with [sp_addrolemember](/sql/relational-databases/system-stored-procedures/sp-addrolemember-transact-sql):
196196

@@ -206,80 +206,80 @@ Connect to your SQL Database using a Microsoft Entra identity that has permissio
206206
GO
207207
```
208208

209-
You can set the *DBOwnerApp* as the Microsoft Entra admin. This can be done using the Azure portal, PowerShell, or Azure CLI commands. For more information, see [Set Microsoft Entra admin](authentication-aad-configure.md#azure-sql-database-and-azure-synapse-analytics).
209+
You can set the *DBOwnerApp* as the Microsoft Entra admin. This can be done using the Azure portal, PowerShell, or Azure CLI commands. For more information, see [Set Microsoft Entra admin](authentication-aad-configure.md#azure-sql-database-and-azure-synapse-analytics).
210210

211-
<a name='create-an-azure-ad-user-in-sql-database-using-an-azure-ad-service-principal'></a>
211+
<a id="create-an-azure-ad-user-in-sql-database-using-an-azure-ad-service-principal"></a>
212212

213213
## Create a user with a service principal
214214

215215
1. Use the following script to create a Microsoft Entra service principal user *myapp* using the service principal *DBOwnerApp*:
216216

217-
- Replace `<TenantId>` with your `TenantId` gathered earlier.
218-
- Replace `<ClientId>` with your `ClientId` gathered earlier.
219-
- Replace `<ClientSecret>` with your client secret created earlier.
220-
- Replace `<ServerName>` with your logical server name. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
221-
- Replace `<database name>` with your SQL Database name.
222-
223-
```powershell
224-
# PowerShell script for creating a new SQL user called myapp using application DBOwnerApp with secret
225-
# DBOwnerApp is an admin for the server
226-
227-
# Download latest MSAL - https://www.powershellgallery.com/packages/MSAL.PS
228-
Import-Module MSAL.PS
229-
230-
$tenantId = "<TenantId>" # Microsoft Entra tenant ID where DBOwnerApp resides
231-
$clientId = "<ClientId>" # Application (client) ID recorded earlier for DBOwnerApp
232-
$clientSecret = "<ClientSecret>" # Client secret for DBOwnerApp
233-
$scopes = "https://database.windows.net/.default" # The endpoint
234-
235-
$result = Get-MsalToken -RedirectUri $uri -ClientId $clientId -ClientSecret (ConvertTo-SecureString $clientSecret -AsPlainText -Force) -TenantId $tenantId -Scopes $scopes
236-
237-
$Tok = $result.AccessToken
238-
#Write-host "token"
239-
$Tok
240-
241-
$SQLServerName = "<ServerName>" # Logical server name
242-
$DatabaseName = "<database name>" # Azure SQL database name
243-
244-
Write-Host "Create SQL connection string"
245-
$conn = New-Object System.Data.SqlClient.SQLConnection
246-
$conn.ConnectionString = "Data Source=$SQLServerName.database.windows.net;Initial Catalog=$DatabaseName;Connect Timeout=30"
247-
$conn.AccessToken = $Tok
248-
249-
Write-host "Connect to database and execute SQL script"
250-
$conn.Open()
251-
$ddlstmt = 'CREATE USER [myapp] FROM EXTERNAL PROVIDER;'
252-
Write-host " "
253-
Write-host "SQL DDL command"
254-
$ddlstmt
255-
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($ddlstmt, $conn)
256-
257-
Write-host "results"
258-
$command.ExecuteNonQuery()
259-
$conn.Close()
260-
```
261-
262-
Alternatively, you can use the following code: [Microsoft Entra service principal authentication to Azure SQL Database](https://techcommunity.microsoft.com/t5/azure-sql-database/azure-ad-service-principal-authentication-to-sql-db-code-sample/ba-p/481467). Modify the script to execute the DDL statement `CREATE USER [myapp] FROM EXTERNAL PROVIDER`. The same script can be used to create a Microsoft Entra user or group in your database.
263-
264-
2. Check if the user *myapp* exists in the database by executing the following command:
265-
266-
```sql
267-
SELECT name, type, type_desc, CAST(CAST(sid as varbinary(16)) as uniqueidentifier) as appId
268-
FROM sys.database_principals
269-
WHERE name = 'myapp'
270-
GO
271-
```
272-
273-
You should see a similar output:
274-
275-
```output
276-
name type type_desc appId
277-
myapp E EXTERNAL_USER 6d228f48-xxxx-xxxx-xxxx-xxxxxxxxxxxx
278-
```
279-
280-
## Next steps
281-
282-
- [Microsoft Entra service principal with Azure SQL](authentication-aad-service-principal.md)
217+
- Replace `<TenantId>` with your `TenantId` gathered earlier.
218+
- Replace `<ClientId>` with your `ClientId` gathered earlier.
219+
- Replace `<ClientSecret>` with your client secret created earlier.
220+
- Replace `<ServerName>` with your logical server name. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
221+
- Replace `<database name>` with your SQL Database name.
222+
223+
```powershell
224+
# PowerShell script for creating a new SQL user called myapp using application DBOwnerApp with secret
225+
# DBOwnerApp is an admin for the server
226+
227+
# Download latest MSAL - https://www.powershellgallery.com/packages/MSAL.PS
228+
Import-Module MSAL.PS
229+
230+
$tenantId = "<TenantId>" # Microsoft Entra tenant ID where DBOwnerApp resides
231+
$clientId = "<ClientId>" # Application (client) ID recorded earlier for DBOwnerApp
232+
$clientSecret = "<ClientSecret>" # Client secret for DBOwnerApp
233+
$scopes = "https://database.windows.net/.default" # The endpoint
234+
235+
$result = Get-MsalToken -RedirectUri $uri -ClientId $clientId -ClientSecret (ConvertTo-SecureString $clientSecret -AsPlainText -Force) -TenantId $tenantId -Scopes $scopes
236+
237+
$Tok = $result.AccessToken
238+
#Write-host "token"
239+
$Tok
240+
241+
$SQLServerName = "<ServerName>" # Logical server name
242+
$DatabaseName = "<database name>" # Azure SQL database name
243+
244+
Write-Host "Create SQL connection string"
245+
$conn = New-Object System.Data.SqlClient.SQLConnection
246+
$conn.ConnectionString = "Data Source=$SQLServerName.database.windows.net;Initial Catalog=$DatabaseName;Connect Timeout=30"
247+
$conn.AccessToken = $Tok
248+
249+
Write-host "Connect to database and execute SQL script"
250+
$conn.Open()
251+
$ddlstmt = 'CREATE USER [myapp] FROM EXTERNAL PROVIDER;'
252+
Write-host " "
253+
Write-host "SQL DDL command"
254+
$ddlstmt
255+
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($ddlstmt, $conn)
256+
257+
Write-host "results"
258+
$command.ExecuteNonQuery()
259+
$conn.Close()
260+
```
261+
262+
Alternatively, you can use the following code: [Microsoft Entra service principal authentication to Azure SQL Database](https://techcommunity.microsoft.com/t5/azure-sql-database/azure-ad-service-principal-authentication-to-sql-db-code-sample/ba-p/481467). Modify the script to execute the DDL statement `CREATE USER [myapp] FROM EXTERNAL PROVIDER`. The same script can be used to create a Microsoft Entra user or group in your database.
263+
264+
1. Check if the user *myapp* exists in the database by executing the following command:
265+
266+
```sql
267+
SELECT name, type, type_desc, CAST(CAST(sid as varbinary(16)) as uniqueidentifier) as appId
268+
FROM sys.database_principals
269+
WHERE name = 'myapp'
270+
GO
271+
```
272+
273+
You should see a similar output:
274+
275+
```output
276+
name type type_desc appId
277+
myapp E EXTERNAL_USER aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
278+
```
279+
280+
## Related content
281+
282+
- [Microsoft Entra service principals with Azure SQL](authentication-aad-service-principal.md)
283283
- [What are managed identities for Azure resources?](/azure/active-directory/managed-identities-azure-resources/overview)
284284
- [How to use managed identities for App Service and Azure Functions](/azure/app-service/overview-managed-identity)
285285
- [Microsoft Entra service principal authentication to SQL DB - Code Sample](https://techcommunity.microsoft.com/t5/azure-sql-database/azure-ad-service-principal-authentication-to-sql-db-code-sample/ba-p/481467)
-484 Bytes
Loading

0 commit comments

Comments
 (0)