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
Copy file name to clipboardExpand all lines: articles/azure-sql/database/authentication-azure-ad-logins-tutorial.md
+47-17Lines changed: 47 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ ms.topic: tutorial
7
7
author: GithubMirek
8
8
ms.author: mireks
9
9
ms.reviewer: vanto
10
-
ms.date: 03/11/2022
10
+
ms.date: 03/14/2022
11
11
---
12
12
13
13
# Tutorial: Create and utilize Azure Active Directory server logins
@@ -25,13 +25,13 @@ In this tutorial, you learn how to:
25
25
> - Create an Azure AD login in the virtual master database with the new syntax extension for Azure SQL Database
26
26
> - Create a user mapped to an Azure AD login in the virtual master database
27
27
> - Grant server roles to an Azure AD user
28
-
> - Disable a login
28
+
> - Disable an Azure AD login
29
29
30
30
## Prerequisites
31
31
32
32
- A SQL Database or SQL Managed Instance with a database. See [Quickstart: Create an Azure SQL Database single database](single-database-create-quickstart.md) if you haven't already created an Azure SQL Database, or [Quickstart: Create an Azure SQL Managed Instance](../managed-instance/instance-create-quickstart.md).
33
33
- Azure AD authentication set up for SQL Database or Managed Instance. For more information, see [Configure and manage Azure AD authentication with Azure SQL](authentication-aad-configure.md).
34
-
-The user creating the login must have Azure Active Directory admin permissions, or have membership in the `loginmanager`server role.
34
+
-This article instructs you on creating an Azure AD login and user within the virtual master database. Only an Azure AD admin can create a user within the virtual master database, so we recommend you use the Azure AD admin account when going through this tutorial. An Azure AD principal with the `loginmanager` role can create a login, but not a user within the virtual master database.
35
35
36
36
## Create Azure AD login
37
37
@@ -70,7 +70,7 @@ In this tutorial, you learn how to:
70
70
71
71
1. Now that we've created an Azure AD login, we can create a database-level Azure AD user that is mapped to the Azure AD login in the virtual master database. We'll continue to use our example, `[email protected]` to create a user in the virtual master database, as we want to demonstrate adding the user to special roles. Only an Azure AD admin or SQL server admin can create users in the virtual master database.
72
72
73
-
1. We're using the virtual master database, but you can switch to a database of your choice. Run the following query.
73
+
1. We're using the virtual master database, but you can switch to a database of your choice if you want to create users in other databases. Run the following query.
74
74
75
75
```sql
76
76
Use master
@@ -100,36 +100,36 @@ In this tutorial, you learn how to:
100
100
>
101
101
> For example, `CREATE USER [[email protected]] FROM EXTERNAL PROVIDER`.
102
102
103
-
## Grant server roles to the Azure AD user
103
+
## Grant roles to the Azure AD login
104
104
105
-
[Special roles for SQL Database](/sql/relational-databases/security/authentication-access/database-level-roles#special-roles-for--and-azure-synapse) can be assigned to users in the virtual master database.
105
+
[Special roles for SQL Database](/sql/relational-databases/security/authentication-access/database-level-roles#special-roles-for--and-azure-synapse) can be assigned to logins in the virtual master database.
106
106
107
-
In order to grant one of the server roles, an Azure AD user with a login must be created in the virtual master database.
107
+
In order to grant one of the special database roles, an Azure AD user with a login must be created in the virtual master database.
108
108
109
109
To add a user to a role, you can run the following query:
110
110
111
111
```sql
112
-
ALTER SERVER ROLE [dbamanger] ADD MEMBER [AzureAD_object]
112
+
ALTER ROLE [dbamanger] ADD MEMBER [AzureAD_object]
113
113
```
114
114
115
115
To remove a user from a role, run the following query:
116
116
117
117
```sql
118
-
ALTER SERVER ROLE [dbamanger] DROP MEMBER [AzureAD_object]
118
+
ALTER ROLE [dbamanger] DROP MEMBER [AzureAD_object]
119
119
```
120
120
121
-
`AzureAD_object` can be an Azure AD user, group, or service principal create in Azure SQL.
121
+
`AzureAD_object` can be an Azure AD user, group, or service principal in Azure AD.
122
122
123
123
In our example, we created the user `[email protected]`. Let's give the user the **dbmanager** and **loginmanager** roles.
124
124
125
125
1. Run the following query:
126
126
127
127
```sql
128
-
ALTER SERVER ROLE [dbamanger] ADD MEMBER [bob@contoso.com]
129
-
ALTER SERVER ROLE [loginmanager] ADD MEMBER [bob@contoso.com]
128
+
ALTER ROLE [dbamanger] ADD MEMBER [bob@contoso.com]
129
+
ALTER ROLE [loginmanager] ADD MEMBER [bob@contoso.com]
130
130
```
131
131
132
-
1. Check the server role assignment by running the following query:
132
+
1. Check the database role assignment by running the following query:
133
133
134
134
```sql
135
135
SELECTDP1.nameAS DatabaseRoleName,
@@ -150,20 +150,42 @@ In our example, we created the user `[email protected]`. Let's give the user the *
You can also choose to give the user additional [built-in server-level roles](security-server-roles.md#built-in-server-level-roles), such as the **##MS_DefinitionReader##**, **##MS_ServerStateReader##**, or **##MS_ServerStateManager##** role.
156
156
157
+
> [!NOTE]
158
+
> The server-level roles mentioned here are not supported for Azure AD groups.
159
+
160
+
```sql
161
+
ALTER SERVER ROLE ##MS_DefinitionReader## ADD MEMBER [AzureAD_object];
162
+
```
163
+
164
+
```sql
165
+
ALTER SERVER ROLE ##MS_ServerStateReader## ADD MEMBER [AzureAD_object];
166
+
```
167
+
157
168
```sql
158
-
ALTER SERVER ROLE ##MS_DefinitionReader## ADD MEMBER [AAD_object];
169
+
ALTER SERVER ROLE ##MS_ServerStateManager## ADD MEMBER [AzureAD_object];
159
170
```
160
171
172
+
Permissions aren't effective until the user reconnects. Flush the DBCC cache as well:
173
+
161
174
```sql
162
-
ALTER SERVER ROLE ##MS_ServerStateReader## ADD MEMBER [AAD_object];
175
+
DBCC FLUSHAUTHCACHE
176
+
DBCC FREESYSTEMCACHE('TokenAndPermUserStore') WITH NO_INFOMSGS
163
177
```
164
178
179
+
To check which Azure AD logins are part of server-level roles, run the following query:
180
+
165
181
```sql
166
-
ALTER SERVER ROLE ##MS_ServerStateManager## ADD MEMBER [AAD_object];
Copy file name to clipboardExpand all lines: articles/azure-sql/database/authentication-azure-ad-logins.md
+60-30Lines changed: 60 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ ms.topic: conceptual
7
7
author: GithubMirek
8
8
ms.author: mireks
9
9
ms.reviewer: vanto
10
-
ms.date: 03/11/2022
10
+
ms.date: 03/14/2022
11
11
---
12
12
13
13
# Azure Active Directory server principals
@@ -17,21 +17,23 @@ ms.date: 03/11/2022
17
17
> [!NOTE]
18
18
> Azure Active Directory (Azure AD) server principals (logins) are currently in public preview for Azure SQL Database. Azure SQL Managed Instance can already utilize Azure AD logins.
19
19
20
-
You can now create and utilize Azure AD server principals, which are logins in the master database of a SQL Database. There are several benefits of using Azure AD server principals for SQL Database:
20
+
You can now create and utilize Azure AD server principals, which are logins in the virutal master database of a SQL Database. There are several benefits of using Azure AD server principals for SQL Database:
21
21
22
-
- Support multiple Azure AD login accounts with high privileged server roles for SQL Database, such as the `loginmanager` and `dbmanager` roles.
23
-
- Increase functional improvement support, such as utilizing [Azure AD-only authentication](authentication-azure-ad-only-authentication.md). Azure AD-only authentication allows SQL authentication to be disabled, which includes the SQL server admin, SQL logins, and users.
22
+
- Support multiple Azure AD login accounts with [special roles for SQL Database](/sql/relational-databases/security/authentication-access/database-level-roles#special-roles-for--and-azure-synapse), such as the `loginmanager` and `dbmanager` roles.
23
+
- Support [Azure SQL Database server roles for permission management](security-server-roles.md).
24
+
- Functional parity between SQL logins and Azure AD logins.
25
+
- Increase functional improvement support, such as utilizing [Azure AD-only authentication](authentication-azure-ad-only-authentication.md). Azure AD-only authentication allows SQL authentication to be disabled, which includes the SQL server admin, SQL logins and users.
24
26
- Allows Azure AD principals to support geo-replicas. Azure AD principals will be able to connect to the geo-replica of a user database, with a *read-only* permission and *deny* permission to the primary server.
25
-
- Ability to use Azure AD service principal logins with high privilege server roles to execute a full automation of user and database creation, as well as maintenance provided by Azure AD applications.
27
+
- Ability to use Azure AD service principal logins with special roles to execute a full automation of user and database creation, as well as maintenance provided by Azure AD applications.
26
28
- Closer functionality between Managed Instance and SQL Database, as Managed Instance already supports Azure AD logins in the master database.
27
29
28
30
For more information on Azure AD authentication in Azure SQL, see [Use Azure Active Directory authentication](authentication-aad-overview.md)
29
31
30
32
## Permissions
31
33
32
-
The following permissions are required to utilize or create Azure AD logins in the master database.
34
+
The following permissions are required to utilize or create Azure AD logins in the virtual master database.
33
35
34
-
- Azure AD admin permission or membership in the `loginmanager` server role.
36
+
- Azure AD admin permission or membership in the `loginmanager` server role. The first Azure AD login can only be created by the Azure AD admin.
35
37
- Must be a member of Azure AD within the same directory used for Azure SQL Database
36
38
37
39
By default, the standard permission granted to newly created Azure AD login in the `master` database is **VIEW ANY DATABASE**.
For more information, see [CREATE LOGIN (Transact-SQL)](/sql/t-sql/statements/create-login-transact-sql?view=azuresqldb-current&preserve-view=true).
55
+
The *login_name* specifies the Azure AD principal, which is an Azure AD user, group, or application.
56
+
57
+
For more information, see [CREATE LOGIN (Transact-SQL)](/sql/t-sql/statements/create-login-transact-sql?view=azuresqldb-current&preserve-view=true). More information about the `WITH OBJECT_ID` clause is explained in [the section below](#azure-ad-logins-and-users-with-non-unique-display-names).
54
58
55
59
### Create user syntax
56
60
57
-
The below T-SQL syntax is already available in SQL Database, and can be used for creating database-level Azure AD principals mapped to Azure AD logins in the master database.
61
+
The below T-SQL syntax is already available in SQL Database, and can be used for creating database-level Azure AD principals mapped to Azure AD logins in the virtual master database.
58
62
59
63
To create an Azure AD user from an Azure AD login, use the following syntax:
60
64
61
65
```syntaxsql
62
66
CREATE USER user_name FROM LOGIN login_name
63
67
```
64
68
65
-
For more information, see [CREATE USER (Transact-SQL)](/sql/t-sql/statements/create-user-transact-sql).
69
+
For more information, see [CREATE USER (Transact-SQL)](/sql/t-sql/statements/create-user-transact-sql). More information about the `WITH OBJECT_ID` clause is explained in [the section below](#azure-ad-logins-and-users-with-non-unique-display-names).
66
70
67
71
### Disable or enable a login using ALTER LOGIN syntax
68
72
@@ -72,7 +76,7 @@ The [ALTER LOGIN (Transact-SQL)](/sql/t-sql/statements/alter-login-transact-sql?
72
76
ALTER LOGIN login_name DISABLE
73
77
```
74
78
75
-
The Azure AD principal `login_name` won't be able to log into any user database in the SQL Database server where an Azure AD user principal, `user_name` mapped to login `login_name` was created.
79
+
The Azure AD principal `login_name` won't be able to log into any user database in the SQL Database logical server where an Azure AD user principal, `user_name` mapped to login `login_name` was created.
76
80
77
81
> [!NOTE]
78
82
> -`ALTER LOGIN login_name DISABLE` is not supported for contained users.
@@ -85,24 +89,17 @@ The Azure AD principal `login_name` won't be able to log into any user database
85
89
> DBCC FREESYSTEMCACHE('TokenAndPermUserStore') WITH NO_INFOMSGS
86
90
>```
87
91
88
-
## Server-level roles for Azure AD principals
89
-
90
-
[Special roles for SQL Database](/sql/relational-databases/security/authentication-access/database-level-roles#special-roles-for--and-azure-synapse) can be assigned to users in the virtual master database for Azure AD principals, including **dbmanager** and **loginmanager**. For more server roles, see [Azure SQL Database server roles for permission management](security-server-roles.md).
92
+
## Roles for Azure AD principals
91
93
92
-
For a tutorial on how to grant these roles to a user, see [Tutorial: Create and utilize Azure Active Directory server logins](authentication-azure-ad-logins-tutorial.md).
93
-
94
-
## Azure AD logins and users with non-unique display names
94
+
[Special roles for SQL Database](/sql/relational-databases/security/authentication-access/database-level-roles#special-roles-for--and-azure-synapse) can be assigned to *users* in the virtual master database for Azure AD principals, including **dbmanager** and **loginmanager**.
95
95
96
-
It's possible to create Azure AD resources with the same display names. For example, creating an [Azure AD application (service principal)](authentication-aad-service-principal.md) with the same name. In this release, we're also introducing the ability to create logins and users using the **Object ID**.
96
+
[Azure SQL Database server roles](security-server-roles.md) can be assigned to *logins* in the virtual master database..
97
97
98
-
```sql
99
-
CREATE LOGIN login_name FROM EXTERNAL PROVIDER WITH OBJECT_ID ='objectid'
100
-
```
98
+
For a tutorial on how to grant these roles, see [Tutorial: Create and utilize Azure Active Directory server logins](authentication-azure-ad-logins-tutorial.md).
101
99
102
-
- To execute the above query, the specified Object ID must exist in Azure AD where the Azure SQL resides.
103
-
- Most non-unique display names in Azure AD are related to service principals. Group names can also be non-unique as well. All Azure AD user display names are unique.
100
+
## Azure AD logins and users with non-unique display names
104
101
105
-
Using the display name of a service principal that isn't unique in Azure AD could lead to errors when creating the login or user in Azure SQL. For example, if `myapp` isn't unique, you may run into the following error when executing the following query:
102
+
Using the display name of a service principal that isn't unique in Azure AD leads to errors when creating the login or user in Azure SQL. For example, if `myapp` isn't unique, you may run into the following error when executing the following query:
106
103
107
104
```sql
108
105
CREATE LOGIN [myapp] FROM EXTERNAL PROVIDER
@@ -113,24 +110,55 @@ Msg 33131, Level 16, State 1, Line 4
113
110
Principal 'myapp' has a duplicate display name. Make the display name unique in Azure Active Directory and execute this statement again.
114
111
```
115
112
116
-
With the T-SQL DDL extension to create logins or users with the Object ID, you can avoid this error and also specify an alias for the login or user created with the Object ID. For example, the following will create a login `myapp4466e` using the application Object ID `4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx`.
113
+
> [!NOTE]
114
+
> The same error would happen with `CREATE USER` with a non-unique name.
115
+
116
+
This happens because it is possible to create Azure AD resources with the same display names. For example, creating an [Azure AD application (service principal)](authentication-aad-service-principal.md) or Azure AD group with the same name. In this release, we're also introducing the ability to create logins and users using the **Object ID** of the Azure resource.
117
+
118
+
```sql
119
+
CREATE LOGIN login_name FROM EXTERNAL PROVIDER WITH OBJECT_ID ='objectid'
120
+
```
121
+
122
+
- To execute the above query, the specified Object ID must exist in Azure AD where the Azure SQL resource resides. Otherwise, the `CREATE` command will fail.
123
+
- Most non-unique display names in Azure AD are related to service principals. Group names can also be non-unique as well. All Azure AD user display names are unique.
124
+
125
+
With the T-SQL DDL extension to create logins or users with the Object ID, you can avoid error *33131* and also specify an alias for the login or user created with the Object ID. For example, the following will create a login `myapp4466e` using the application Object ID `4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx`.
117
126
118
127
```sql
119
128
CREATE LOGIN [myapp4466e] FROM EXTERNAL PROVIDER
120
129
WITH OBJECT_ID='4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx'
121
130
```
122
131
132
+
> [!TIP]
133
+
> If you're looking to create a contained database user using the OBJECT ID, the command would be:
134
+
>
135
+
> ```sql
136
+
> CREATE USER [myapp4466e] FROM EXTERNAL PROVIDER
137
+
> WITH OBJECT_ID='4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx'
138
+
>```
139
+
123
140
For more information on obtaining the Object ID of a service principal, see [Service principal object](/azure/active-directory/develop/app-objects-and-service-principals#service-principal-object.)
124
141
125
-
To get the Object ID of the application, you can execute the following query:
142
+
### Identify the user created for the application
126
143
127
-
```sql
128
-
SELECT CAST(sid as uniqueidentifier) ApplicationID fromsys.server_principalsWHERE NAME ='myapp4466e'
129
-
```
144
+
It's important to verify the Azure AD alias is tied to the correct application or group. To check that the user was created for the correct service principal (application) or Azure AD group:
145
+
146
+
1. Get the **Application ID** of the application, or **Object ID** of the Azure AD group from the user created in SQL Database by executing the following query:
147
+
148
+
```sql
149
+
SELECT CAST(sid as uniqueidentifier) AzureID fromsys.server_principalsWHERE NAME ='myapp4466e'
150
+
```
151
+
152
+
`AzureID` corresponds to the *Applicaiton ID* for the service principal or *Object ID* for the Azure AD group.
153
+
154
+
1. Go to the [Azure portal](https://portal.azure.com), and in your **Enterprise Application** or Azure AD group resource, check the **Application ID** or **Object ID** respectively. See if it matches the one obtained from the above query.
155
+
156
+
> [!NOTE]
157
+
> When creating a user from a service principal, the **Object ID** is required when using the `WITH OBJECT_ID` clause with the `CREATE` T-SQL statement. This is different from the **Application ID** that is returned when you are trying to verify the alias in Azure SQL. Using this verification process, you can identify the main owner of the SQL alias in Azure AD, and prevent possible mistakes when creating logins or users with an Object ID.
130
158
131
159
## Limitations and remarks
132
160
133
-
- The SQL server admin can’t create Azure AD logins in the master database
161
+
- The SQL server admin can’t create Azure AD logins or users in any databases.
134
162
- Changing a database ownership to an Azure AD group as database owner isn't supported.
135
163
-`ALTER AUTHORIZATION ON database::<mydb> TO [my_aad_group]` fails with an error message:
136
164
```output
@@ -152,6 +180,8 @@ SELECT CAST(sid as uniqueidentifier) ApplicationID from sys.server_principals WH
152
180
- `GRANT <PERMISSION> ON LOGIN :: <Azure AD account> TO <Any other login> `
153
181
- When permissions are altered for an Azure AD login with existing open connections to an Azure SQL Database, permissions aren't effective until the user reconnects. This applies to server role membership change using the [ALTER SERVER ROLE](/sql/t-sql/statements/alter-server-role-transact-sql) statement.
154
182
- [SQL Server Management Studio (SSMS)](/sql/ssms/download-sql-server-management-studio-ssms) doesn't display the login names in **Object Explorer**.
183
+
- Setting an Azure AD login mapped to an Azure AD group as the database owner is not supported.
184
+
- [Azure SQL Database server roles](security-server-roles.md) are not supported for Azure AD groups.
0 commit comments