Skip to content

Commit ed64413

Browse files
authored
Merge pull request #146624 from VanMSFT/updateAADPrinScript
Update script
2 parents 66e907a + c845f74 commit ed64413

File tree

2 files changed

+22
-39
lines changed

2 files changed

+22
-39
lines changed

articles/azure-sql/database/authentication-aad-service-principal-tutorial.md

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.topic: tutorial
88
author: GithubMirek
99
ms.author: mireks
1010
ms.reviewer: vanto
11-
ms.date: 10/21/2020
11+
ms.date: 02/11/2021
1212
---
1313

1414
# Tutorial: Create Azure AD users using Azure AD applications
@@ -228,35 +228,27 @@ Once a service principal is created in Azure AD, create the user in SQL Database
228228

229229
```powershell
230230
# PowerShell script for creating a new SQL user called myapp using application AppSP with secret
231-
232-
$tenantId = "<TenantId>" # tenantID (Azure Directory ID) were AppSP resides
233-
$clientId = "<ClientId>" # AppID also ClientID for AppSP
234-
$clientSecret = "<ClientSecret>" # client secret for AppSP
235-
$Resource = "https://database.windows.net/"
231+
# AppSP is part of an Azure AD admin for the Azure SQL server below
236232
237-
$adalPath = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.8.3"
238-
# To install the latest AzureRM.profile version execute -Install-Module -Name AzureRM.profile
239-
$adal = "$adalPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
240-
$adalforms = "$adalPath\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
241-
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
242-
$resourceAppIdURI = 'https://database.windows.net/'
243-
244-
# Set Authority to Azure AD Tenant
245-
$authority = 'https://login.windows.net/' + $tenantId
246-
247-
$ClientCred = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($clientId, $clientSecret)
248-
$authContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($authority)
249-
$authResult = $authContext.AcquireTokenAsync($resourceAppIdURI,$ClientCred)
250-
$Tok = $authResult.Result.CreateAuthorizationHeader()
251-
$Tok=$Tok.Replace("Bearer ","")
252-
Write-host "token"
253-
$Tok
254-
Write-host " "
255-
233+
# Download latest MSAL - https://www.powershellgallery.com/packages/MSAL.PS
234+
Import-Module MSAL.PS
235+
236+
$tenantId = "<TenantId>" # tenantID (Azure Directory ID) were AppSP resides
237+
$clientId = "<ClientId>" # AppID also ClientID for AppSP
238+
$clientSecret = "<ClientSecret>" # Client secret for AppSP
239+
$scopes = "https://database.windows.net/.default" # The end-point
240+
241+
$result = Get-MsalToken -RedirectUri $uri -ClientId $clientId -ClientSecret (ConvertTo-SecureString $clientSecret -AsPlainText -Force) -TenantId $tenantId -Scopes $scopes
242+
243+
$Tok = $result.AccessToken
244+
#Write-host "token"
245+
$Tok
246+
256247
$SQLServerName = "<server name>" # Azure SQL logical server name
257-
Write-Host "Create SQL connectionstring"
258-
$conn = New-Object System.Data.SqlClient.SQLConnection
259248
$DatabaseName = "<database name>" # Azure SQL database name
249+
250+
Write-Host "Create SQL connection string"
251+
$conn = New-Object System.Data.SqlClient.SQLConnection
260252
$conn.ConnectionString = "Data Source=$SQLServerName.database.windows.net;Initial Catalog=$DatabaseName;Connect Timeout=30"
261253
$conn.AccessToken = $Tok
262254
@@ -270,20 +262,11 @@ Once a service principal is created in Azure AD, create the user in SQL Database
270262
271263
Write-host "results"
272264
$command.ExecuteNonQuery()
273-
$conn.Close()
265+
$conn.Close()
274266
```
275267

276268
Alternatively, you can use the code sample in the blog, [Azure AD 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). Modify the script to execute a DDL statement `CREATE USER [myapp] FROM EXTERNAL PROVIDER`. The same script can be used to create a regular Azure AD user a group in SQL Database.
277269

278-
> [!NOTE]
279-
> If you need to install the module AzureRM.profile, you will need to open PowerShell as an administrator. You can use the following commands to automatically install the latest AzureRM.profile version, and set `$adalpath` for the above script:
280-
>
281-
> ```powershell
282-
> Install-Module AzureRM.profile -force
283-
> Import-Module AzureRM.profile
284-
> $version = (Get-Module -Name AzureRM.profile).Version.toString()
285-
> $adalPath = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\${version}"
286-
> ```
287270

288271
2. Check if the user *myapp* exists in the database by executing the following command:
289272

articles/azure-sql/database/authentication-aad-service-principal.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.topic: conceptual
88
author: GithubMirek
99
ms.author: mireks
1010
ms.reviewer: vanto
11-
ms.date: 10/21/2020
11+
ms.date: 02/11/2021
1212
---
1313

1414
# Azure Active Directory service principal with Azure SQL
@@ -47,7 +47,7 @@ Supporting this functionality is useful in Azure AD application automation proce
4747

4848
To enable an Azure AD object creation in SQL Database and Azure Synapse on behalf of an Azure AD application, the following settings are required:
4949

50-
1. Assign the server identity
50+
1. Assign the server identity. The assigned server identity represents the Managed System Identity (MSI). Currently, the server identity for Azure SQL does not support User Managed Identity (UMI).
5151
- For a new Azure SQL logical server, execute the following PowerShell command:
5252

5353
```powershell

0 commit comments

Comments
 (0)