Skip to content

Commit 8d6466c

Browse files
committed
Worked with Mirek on changes
1 parent 2e46545 commit 8d6466c

File tree

3 files changed

+110
-50
lines changed

3 files changed

+110
-50
lines changed

articles/azure-sql/database/authentication-azure-ad-logins-tutorial.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.topic: tutorial
77
author: GithubMirek
88
ms.author: mireks
99
ms.reviewer: vanto
10-
ms.date: 03/11/2022
10+
ms.date: 03/14/2022
1111
---
1212

1313
# Tutorial: Create and utilize Azure Active Directory server logins
@@ -25,13 +25,13 @@ In this tutorial, you learn how to:
2525
> - Create an Azure AD login in the virtual master database with the new syntax extension for Azure SQL Database
2626
> - Create a user mapped to an Azure AD login in the virtual master database
2727
> - Grant server roles to an Azure AD user
28-
> - Disable a login
28+
> - Disable an Azure AD login
2929
3030
## Prerequisites
3131

3232
- 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).
3333
- 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.
3535

3636
## Create Azure AD login
3737

@@ -70,7 +70,7 @@ In this tutorial, you learn how to:
7070

7171
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.
7272

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.
7474

7575
```sql
7676
Use master
@@ -100,36 +100,36 @@ In this tutorial, you learn how to:
100100
>
101101
> For example, `CREATE USER [[email protected]] FROM EXTERNAL PROVIDER`.
102102
103-
## Grant server roles to the Azure AD user
103+
## Grant roles to the Azure AD login
104104

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.
106106

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.
108108

109109
To add a user to a role, you can run the following query:
110110

111111
```sql
112-
ALTER SERVER ROLE [dbamanger] ADD MEMBER [AzureAD_object]
112+
ALTER ROLE [dbamanger] ADD MEMBER [AzureAD_object]
113113
```
114114

115115
To remove a user from a role, run the following query:
116116

117117
```sql
118-
ALTER SERVER ROLE [dbamanger] DROP MEMBER [AzureAD_object]
118+
ALTER ROLE [dbamanger] DROP MEMBER [AzureAD_object]
119119
```
120120

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.
122122

123123
In our example, we created the user `[email protected]`. Let's give the user the **dbmanager** and **loginmanager** roles.
124124

125125
1. Run the following query:
126126

127127
```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]
130130
```
131131

132-
1. Check the server role assignment by running the following query:
132+
1. Check the database role assignment by running the following query:
133133

134134
```sql
135135
SELECT DP1.name AS DatabaseRoleName,
@@ -150,20 +150,42 @@ In our example, we created the user `[email protected]`. Let's give the user the *
150150
loginmanager [email protected]
151151
```
152152

153-
### Additional server-level roles
153+
### Server-level roles
154154

155155
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.
156156

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+
157168
```sql
158-
ALTER SERVER ROLE ##MS_DefinitionReader## ADD MEMBER [AAD_object];
169+
ALTER SERVER ROLE ##MS_ServerStateManager## ADD MEMBER [AzureAD_object];
159170
```
160171

172+
Permissions aren't effective until the user reconnects. Flush the DBCC cache as well:
173+
161174
```sql
162-
ALTER SERVER ROLE ##MS_ServerStateReader## ADD MEMBER [AAD_object];
175+
DBCC FLUSHAUTHCACHE
176+
DBCC FREESYSTEMCACHE('TokenAndPermUserStore') WITH NO_INFOMSGS
163177
```
164178

179+
To check which Azure AD logins are part of server-level roles, run the following query:
180+
165181
```sql
166-
ALTER SERVER ROLE ##MS_ServerStateManager## ADD MEMBER [AAD_object];
182+
SELECT roles.principal_id AS RolePID,roles.name AS RolePName,
183+
server_role_members.member_principal_id AS MemberPID, members.name AS MemberPName
184+
FROM sys.server_role_members AS server_role_members
185+
INNER JOIN sys.server_principals AS roles
186+
ON server_role_members.role_principal_id = roles.principal_id
187+
INNER JOIN sys.server_principals AS members
188+
ON server_role_members.member_principal_id = members.principal_id;
167189
```
168190

169191
## Optional - Disable a login
@@ -174,6 +196,14 @@ The [ALTER LOGIN (Transact-SQL)](/sql/t-sql/statements/alter-login-transact-sql?
174196
ALTER LOGIN [bob@contoso.com] DISABLE
175197
```
176198

199+
Check that the login has been disabled by executing the following query:
200+
201+
```sql
202+
SELECT name, type_desc, type
203+
FROM sys.server_principals
204+
WHERE is_disabled = 1
205+
```
206+
177207
A use case for this would be to allow read-only on [geo-replicas](active-geo-replication-overview.md), but deny connection on a primary server.
178208

179209
## See also

articles/azure-sql/database/authentication-azure-ad-logins.md

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.topic: conceptual
77
author: GithubMirek
88
ms.author: mireks
99
ms.reviewer: vanto
10-
ms.date: 03/11/2022
10+
ms.date: 03/14/2022
1111
---
1212

1313
# Azure Active Directory server principals
@@ -17,21 +17,23 @@ ms.date: 03/11/2022
1717
> [!NOTE]
1818
> 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.
1919
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:
2121

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.
2426
- 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.
2628
- Closer functionality between Managed Instance and SQL Database, as Managed Instance already supports Azure AD logins in the master database.
2729

2830
For more information on Azure AD authentication in Azure SQL, see [Use Azure Active Directory authentication](authentication-aad-overview.md)
2931

3032
## Permissions
3133

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.
3335

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.
3537
- Must be a member of Azure AD within the same directory used for Azure SQL Database
3638

3739
By default, the standard permission granted to newly created Azure AD login in the `master` database is **VIEW ANY DATABASE**.
@@ -50,19 +52,21 @@ CREATE LOGIN login_name { FROM EXTERNAL PROVIDER [WITH OBJECT_ID = 'objectid'] |
5052
    | , SID = sid, ]
5153
```
5254

53-
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).
5458

5559
### Create user syntax
5660

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.
5862

5963
To create an Azure AD user from an Azure AD login, use the following syntax:
6064

6165
```syntaxsql
6266
CREATE USER user_name FROM LOGIN login_name
6367
```
6468

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).
6670

6771
### Disable or enable a login using ALTER LOGIN syntax
6872

@@ -72,7 +76,7 @@ The [ALTER LOGIN (Transact-SQL)](/sql/t-sql/statements/alter-login-transact-sql?
7276
ALTER LOGIN login_name DISABLE
7377
```
7478

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.
7680

7781
> [!NOTE]
7882
> - `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
8589
> DBCC FREESYSTEMCACHE('TokenAndPermUserStore') WITH NO_INFOMSGS
8690
> ```
8791
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
9193
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**.
9595
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..
9797
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).
10199
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
104101
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:
106103
107104
```sql
108105
CREATE LOGIN [myapp] FROM EXTERNAL PROVIDER
@@ -113,24 +110,55 @@ Msg 33131, Level 16, State 1, Line 4
113110
Principal 'myapp' has a duplicate display name. Make the display name unique in Azure Active Directory and execute this statement again.
114111
```
115112

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`.
117126

118127
```sql
119128
CREATE LOGIN [myapp4466e] FROM EXTERNAL PROVIDER
120129
WITH OBJECT_ID='4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx'
121130
```
122131

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+
123140
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.)
124141
125-
To get the Object ID of the application, you can execute the following query:
142+
### Identify the user created for the application
126143
127-
```sql
128-
SELECT CAST(sid as uniqueidentifier) ApplicationID from sys.server_principals WHERE 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 from sys.server_principals WHERE 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.
130158
131159
## Limitations and remarks
132160

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.
134162
- Changing a database ownership to an Azure AD group as database owner isn't supported.
135163
- `ALTER AUTHORIZATION ON database::<mydb> TO [my_aad_group]` fails with an error message:
136164
```output
@@ -152,6 +180,8 @@ SELECT CAST(sid as uniqueidentifier) ApplicationID from sys.server_principals WH
152180
- `GRANT <PERMISSION> ON LOGIN :: <Azure AD account> TO <Any other login> `
153181
- 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.
154182
- [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.
155185
156186
## Next steps
157187

0 commit comments

Comments
 (0)