I'm trying to set up AAD authentication to Azure SQL from multi-tenant AAD application and another tenant then where database is. For this I created:
- An AAD multi-tenant application
test-multitenant
in tenant A - A service principal in tenant B for application
test-multitenant
. - Azure SQL database
test-db
in subscription which is in tenant B. - A security group
test-group
in tenant B and set it as AAD administrator for SQL server (test-server
) of databasetest-db
. - Add application
test-multitenant
service principal in tenant B totest-group
security group. So, it has all permissions oftest-group
security group. - Created this PowerShell script to test connectivity
# get db token
$clientId = '<test-multitenant-app-id>' # test-multitenant
$clientSecret = '<test-multitenant-app-secret>' # test-multitenant
$credentials = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($clientId, $clientSecret)
$tenant = '<tenant-A-id>' # test-multitenant
$authority = "https://login.windows.net/$tenant"
$context = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($authority)
$authTokenTask = $context.AcquireTokenAsync('https://database.windows.net/', $credentials)
$token = $authTokenTask.GetAwaiter().GetResult().AccessToken
# connect
$connectionString = 'Server=test-server.database.windows.net;Initial Catalog=test-db;Integrated Security=false;'
$connection = [System.Data.SqlClient.SQLConnection]::new($connectionString)
$connection.AccessToken = $token
$command = [System.Data.SqlClient.SqlCommand]::new('select count(*) from [dbo].[test]', $connection)
$connection.Open()
$result = $command.ExecuteScalar()
"Result: $result"
And unfortunatelly I'm getting this error
Login failed for user '<token-identified principal>'.
But interesting if to use application from tenant B and do the same everything works fine.
Does anybody know whether this scenario is supported by Azure SQL and AAD? Thank you