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
title: Create Microsoft Entra users using service principals
2
+
title: Create Microsoft Entra Users Using Service Principals
3
3
description: This tutorial walks you through creating Microsoft Entra users with a Microsoft Entra application (service principal) in Azure SQL Database.
4
4
author: VanMSFT
5
5
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
8
8
ms.service: azure-sql-database
9
9
ms.subservice: security
10
10
ms.topic: tutorial
@@ -17,7 +17,7 @@ ms.custom:
17
17
18
18
# Tutorial: Create Microsoft Entra users using Microsoft Entra applications
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.
23
23
@@ -38,54 +38,54 @@ In this tutorial, you learn how to:
38
38
39
39
- An existing [Azure SQL Database](single-database-create-quickstart.md) deployment. We assume you have a working SQL Database for this tutorial.
40
40
- 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.
42
42
- The latest version of the [Microsoft.Graph](https://www.powershellgallery.com/packages/Microsoft.Graph) PowerShell module.
43
43
44
44
## Assign an identity to the logical server
45
45
46
46
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:
47
47
48
-
- Replace `<TenantId>` with your **Tenant ID**.
48
+
- Replace `<TenantId>` with your **Tenant ID**.
49
49
50
-
```powershell
51
-
Connect-AzAccount -Tenant <TenantId>
52
-
```
50
+
```powershell
51
+
Connect-AzAccount -Tenant <TenantId>
52
+
```
53
53
54
-
Record the `TenantId` for future use in this tutorial.
54
+
Record the `TenantId` for future use in this tutorial.
55
55
56
56
1. Generate a system-assigned managed identity and assign it to the [logical server in Azure](logical-servers.md). Execute the following PowerShell command:
57
57
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`.
1. Check the server identity was successfully assigned. Execute the following PowerShell command:
65
65
66
-
- Replace `<ResourceGroupName>` and `<ServerName>` with your resources. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
- Replace `<ResourceGroupName>` and `<ServerName>` with your resources. If your server name is `myserver.database.windows.net`, replace `<ServerName>` with `myserver`.
72
67
73
-
Your output should show you `PrincipalId`, `Type`, and `TenantId`. The identity assigned is the `PrincipalId`.
Your output should show you `PrincipalId`, `Type`, and `TenantId`. The identity assigned is the `PrincipalId`.
74
74
75
75
1. You can also check the identity by going to the [Azure portal](https://portal.azure.com).
76
76
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.
78
78
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.":::
80
80
81
81
## Add server identity to Directory Readers role
82
82
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.
84
84
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:
> This script must be executed by a Microsoft Entra `Privileged Role Administrator` or higher role.
@@ -146,13 +146,13 @@ if ($isDirReader -eq $null) {
146
146
```
147
147
148
148
> [!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.
150
150
151
151
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).
152
152
153
153
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).
@@ -164,11 +164,11 @@ To register your applications:
164
164
165
165
1. In the Azure portal, select **Microsoft Entra ID** > **App registrations** > **New registration**.
166
166
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.":::
168
168
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.
170
170
171
-

171
+

172
172
173
173
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.
174
174
@@ -185,12 +185,12 @@ Connect to your SQL Database using a Microsoft Entra identity that has permissio
185
185
186
186
1. Create the user *DBOwnerApp* in the SQL Database using the following T-SQL command:
187
187
188
-
```sql
189
-
CREATE USER [DBOwnerApp] FROM EXTERNAL PROVIDER
190
-
GO
191
-
```
188
+
```sql
189
+
CREATE USER [DBOwnerApp] FROM EXTERNAL PROVIDER
190
+
GO
191
+
```
192
192
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.
194
194
195
195
You can add *DBOwnerApp* to the `db_owner` role with [sp_addrolemember](/sql/relational-databases/system-stored-procedures/sp-addrolemember-transact-sql):
196
196
@@ -206,80 +206,80 @@ Connect to your SQL Database using a Microsoft Entra identity that has permissio
206
206
GO
207
207
```
208
208
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).
1. Use the following script to create a Microsoft Entra service principal user *myapp* using the service principal *DBOwnerApp*:
216
216
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
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
-
nametypetype_descappId
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
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
+
FROMsys.database_principals
269
+
WHERE name ='myapp'
270
+
GO
271
+
```
272
+
273
+
You should see a similar output:
274
+
275
+
```output
276
+
nametypetype_descappId
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)
283
283
-[What are managed identities for Azure resources?](/azure/active-directory/managed-identities-azure-resources/overview)
284
284
-[How to use managed identities for App Service and Azure Functions](/azure/app-service/overview-managed-identity)
285
285
-[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)
0 commit comments