Create Azure AD guest users and set as an Azure AD admin
Applies to:
Azure SQL Database
Azure SQL Managed Instance
Guest users in Azure Active Directory (Azure AD) are users that have been imported into the current Azure AD from other Azure Active Directories, or outside of it. For example, guest users can include users from other Azure Active Directories, or from accounts like @outlook.com, @hotmail.com, @live.com, or @gmail.com.
This article demonstrates how to create an Azure AD guest user and set that user as an Azure AD admin for Azure SQL Managed Instance or the logical server in Azure used by Azure SQL Database and Azure Synapse Analytics, without having to add the guest user to a group inside Azure AD.
Feature description
This feature lifts the current limitation that only allows guest users to connect to Azure SQL Database, SQL Managed Instance, or Azure Synapse Analytics when they're members of a group created in Azure AD. The group needed to be mapped to a user manually using the CREATE USER (Transact-SQL) statement in a given database. Once a database user has been created for the Azure AD group containing the guest user, the guest user can sign into the database using Azure Active Directory with MFA authentication. Guest users can be created and connect directly to SQL Database, SQL Managed Instance, or Azure Synapse without the requirement of adding them to an Azure AD group first, and then creating a database user for that Azure AD group.
As part of this feature, you also have the ability to set the Azure AD guest user directly as an AD admin for the logical server or for a managed instance. The existing functionality (which allows the guest user to be part of an Azure AD group that can then be set as the Azure AD admin for the logical server or managed instance) is not impacted. Guest users in the database that are a part of an Azure AD group are also not impacted by this change.
For more information about existing support for guest users using Azure AD groups, see Using multi-factor Azure Active Directory authentication.
Prerequisite
- Az.Sql 2.9.0 module or higher is needed when using PowerShell to set a guest user as an Azure AD admin for the logical server or managed instance.
Create database user for Azure AD guest user
Follow these steps to create a database user using an Azure AD guest user.
Create guest user in SQL Database and Azure Synapse
Ensure that the guest user (for example,
user1@gmail.com
) is already added into your Azure AD and an Azure AD admin has been set for the database server. Having an Azure AD admin is required for Azure Active Directory authentication.Connect to the SQL database as the Azure AD admin or an Azure AD user with sufficient SQL permissions to create users, and run the below command on the database where the guest user needs to be added:
CREATE USER [user1@gmail.com] FROM EXTERNAL PROVIDER
There should now be a database user created for the guest user
user1@gmail.com
.Run the below command to verify the database user got created successfully:
SELECT * FROM sys.database_principals
Disconnect and sign into the database as the guest user
user1@gmail.com
using SQL Server Management Studio (SSMS) using the authentication method Azure Active Directory - Universal with MFA. For more information, see Using multi-factor Azure Active Directory authentication.
Create guest user in SQL Managed Instance
Note
SQL Managed Instance supports logins for Azure AD users, as well as Azure AD contained database users. The below steps show how to create a login and user for an Azure AD guest user in SQL Managed Instance. You can also choose to create a contained database user in SQL Managed Instance by using the method in the Create guest user in SQL Database and Azure Synapse section.
Ensure that the guest user (for example,
user1@gmail.com
) is already added into your Azure AD and an Azure AD admin has been set for the SQL Managed Instance server. Having an Azure AD admin is required for Azure Active Directory authentication.Connect to the SQL Managed Instance server as the Azure AD admin or an Azure AD user with sufficient SQL permissions to create users, and run the following command on the
master
database to create a login for the guest user:CREATE LOGIN [user1@gmail.com] FROM EXTERNAL PROVIDER
There should now be a login created for the guest user
user1@gmail.com
in themaster
database.Run the below command to verify the login got created successfully:
SELECT * FROM sys.server_principals
Run the below command on the database where the guest user needs to be added:
CREATE USER [user1@gmail.com] FROM LOGIN [user1@gmail.com]
There should now be a database user created for the guest user
user1@gmail.com
.Disconnect and sign into the database as the guest user
user1@gmail.com
using SQL Server Management Studio (SSMS) using the authentication method Azure Active Directory - Universal with MFA. For more information, see Using multi-factor Azure Active Directory authentication.
Setting a guest user as an Azure AD admin
Set the Azure AD admin using either the Azure portal, Azure PowerShell, or the Azure CLI.
Azure portal
To setup an Azure AD admin for a logical server or a managed instance using the Azure portal, follow these steps:
- Open the Azure portal.
- Navigate to your SQL server or managed instance Azure Active Directory settings.
- Select Set Admin.
- In the Azure AD pop-up prompt, type the guest user, such as
guestuser@gmail.com
. - Select this new user, and then save the operation.
For more information, see Setting Azure AD admin.
Azure PowerShell (SQL Database and Azure Synapse)
To setup an Azure AD guest user for a logical server, follow these steps:
Ensure that the guest user (for example,
user1@gmail.com
) is already added into your Azure AD.Run the following PowerShell command to add the guest user as the Azure AD admin for your logical server:
- Replace
<ResourceGroupName>
with your Azure Resource Group name that contains the logical server. - Replace
<ServerName>
with your logical server name. If your server name ismyserver.database.windows.net
, replace<Server Name>
withmyserver
. - Replace
<DisplayNameOfGuestUser>
with your guest user name.
Set-AzSqlServerActiveDirectoryAdministrator -ResourceGroupName <ResourceGroupName> -ServerName <ServerName> -DisplayName <DisplayNameOfGuestUser>
- Replace
You can also use the Azure CLI command az sql server ad-admin to set the guest user as an Azure AD admin for your logical server.
Azure PowerShell (SQL Managed Instance)
To setup an Azure AD guest user for a managed instance, follow these steps:
Ensure that the guest user (for example,
user1@gmail.com
) is already added into your Azure AD.Go to the Azure portal, and go to your Azure Active Directory resource. Under Manage, go to the Users pane. Select your guest user, and record the
Object ID
.Run the following PowerShell command to add the guest user as the Azure AD admin for your SQL Managed Instance:
- Replace
<ResourceGroupName>
with your Azure Resource Group name that contains the SQL Managed Instance. - Replace
<ManagedInstanceName>
with your SQL Managed Instance name. - Replace
<DisplayNameOfGuestUser>
with your guest user name. - Replace
<AADObjectIDOfGuestUser>
with theObject ID
gathered earlier.
Set-AzSqlInstanceActiveDirectoryAdministrator -ResourceGroupName <ResourceGroupName> -InstanceName "<ManagedInstanceName>" -DisplayName <DisplayNameOfGuestUser> -ObjectId <AADObjectIDOfGuestUser>
- Replace
You can also use the Azure CLI command az sql mi ad-admin to set the guest user as an Azure AD admin for your managed instance.
Next steps
Feedback
Submit and view feedback for