Home
%3CLINGO-SUB%20id%3D%22lingo-sub-369078%22%20slang%3D%22en-US%22%3EAzure%20SQL%20Database%20Token-based%20authentication%20with%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-369078%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Oct%2026%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20How%20to%20connect%20to%20Azure%20SQL%20Database%20using%20token-based%20authentication%20in%20PowerShell%20native%20apps%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20This%20guide%20assumes%20you%20already%20have%20a%20deployment%20of%20an%20Azure%20SQL%20Database%2C%20your%20PowerShell%20environment%20configured%20and%20you%20have%20an%20app%20registration%20for%20a%20native%20app%20in%20Azure%20Active%20Directory.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20How%20to%20configure%20a%20native%20app%20registration%3A%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fazuresqldbsupport%2F2018%2F10%2F30%2Fhow-to-create-a-native-powershell-app-registration-to-connect-to-azure-sql-db%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fblogs.msdn.microsoft.com%2Fazuresqldbsupport%2F2018%2F10%2F30%2Fhow-to-create-a-native-powershell-app-registration-to-connect-to-azure-sql-db%2F%20%3CBR%20%2F%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Problem%20summary%3A%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20There%20might%20come%20a%20time%20when%20you%20want%20to%20connect%20to%20your%20database%20with%20token-based%20authentication%20for%20some%20business%20need%20or%20for%20automation%20purposes.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EExplanation%20of%20this%20guide%3A%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20This%20guide%20will%20explain%20how%20to%20connect%20to%20Azure%20SQL%20Database%20using%20token-based%20authentication%20in%20PowerShell%20using%20Native%20application%20registrations.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3ETenant%20Name%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20First%2C%20we%20need%20to%20determine%20what%20our%20AAD%20Directory%20ID%20is.%20For%20that%2C%20please%20go%20to%20your%20Azure%20Active%20Directory%20blade%20and%20go%20to%20%3CSTRONG%3E%20Properties.%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%24tenantName%20%3D%20%22%7BAAD%20DIRECTORY%20ID%7D%22%20%23%20Tenant%20Name%20this%20is%20found%20in%20AAD%20%26gt%3B%20Properties%20%26gt%3B%20Directory%20ID%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Application%20ID%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Now%20we%20need%20to%20determine%20what%20our%20Application%20ID%20is%20and%20for%20that%20we%20also%20need%20to%20create%20a%20new%20App%20Registration%20and%20provide%20the%20proper%20permissions.%20%3CBR%20%2F%3E%20In%20your%20Azure%20Active%20Directory%2C%20go%20to%20App%20Registrations%20and%20create%20a%20new%20%2BNew%20application%20registration.%20%3CBR%20%2F%3E%20Please%20capture%20the%20Application%20ID%20field%20and%20change%20it%20for%20the%20%7BAPPLICATION%20ID%7D%20in%20the%20code%20snippet.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%24clientId%20%3D%20%22%7BAPPLICATION%20ID%7D%22%20%23%20Application%20ID%20is%20found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Application%20ID%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Redirection%20URI%20and%20Resource%20URI%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20The%20redirection%20URI%20is%20determined%20from%20your%20portal%2C%20if%20it's%20a%20native%20Application%20I'd%20suggest%20you%20to%20put%20https%3A%2F%2Flocalhost%2F.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%23%20Rediret%20URI%20is%20found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Settings%20%26gt%3B%20Redirect%20URIs%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24redirectUri%26nbsp%3B%20%3D%20%22%7BREDIRECT%20URI%7D%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Resource%20URI%20is%20the%20resource%20that%20you%20are%20trying%20to%20connect%20to.%20%3CBR%20%2F%3E%20%23%20Resource%20URI%20%26gt%3B%20Found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Settings%20%26gt%3B%20Required%20Permissions%20%26gt%3B%20%3CBR%20%2F%3E%20%23%20Add%20%26gt%3B%20Search%20Bar%20*Type%20in*%20%26gt%3B%20Azure%20SQL%20DB%20%26gt%3B%20Add%20Rights%20%26gt%3B%20OK%20%26gt%3B%20%3CBR%20%2F%3E%20%23%20OK%20%26gt%3B%20Grant%20permissions%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24resourceUri%26nbsp%3B%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fdatabase.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdatabase.windows.net%2F%3C%2FA%3E%22%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Authority%20URI%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20This%20is%20the%20STS%20link%20that%20we%20will%20use%20to%20authenticate%20our%20Tenant.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%24authorityUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%3C%2FA%3E%22%20%23%20Authority%20URI%20%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%7BDirectoryId%7CTenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%7BDirectoryId%7CTenantName%3C%2FA%3E%7D%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Script%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%24tenantName%20%3D%20%22%7BAAD%20DIRECTORY%20ID%7D%22%20%23%20Tenant%20Name%20this%20is%20found%20in%20AAD%20%26gt%3B%20Properties%20%26gt%3B%20Directory%20ID%20%3CBR%20%2F%3E%20%24clientId%20%3D%20%22%7BAPPLICATION%20ID%7D%22%20%23%20Application%20ID%20is%20found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Application%20ID%20%3CBR%20%2F%3E%20%24redirectUri%20%3D%20%22%7BREDIRECT%20URI%7D%22%20%23%20Rediret%20URI%20is%20found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Settings%20%26gt%3B%20Redirect%20URIs%20%3CBR%20%2F%3E%20%24resourceUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fdatabase.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdatabase.windows.net%2F%3C%2FA%3E%22%20%23%20Resource%20URI%20%26gt%3B%20Found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Settings%20%26gt%3B%20Required%20Permissions%20%26gt%3B%20%3CBR%20%2F%3E%20%23%20Add%20%26gt%3B%20Search%20Bar%20*Type%20in*%20%26gt%3B%20Azure%20SQL%20DB%20%26gt%3B%20Add%20Rights%20%26gt%3B%20OK%20%26gt%3B%20OK%20%26gt%3B%20Grant%20permissions%20%3CBR%20%2F%3E%20%24authorityUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%3C%2FA%3E%22%20%23%20Authority%20URI%20%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%7BDirectoryId%7CTenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%7BDirectoryId%7CTenantName%3C%2FA%3E%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Credit%20to%20Ray%20Held%20for%20this%20function%2C%20great%20stuff!%20%3CBR%20%2F%3E%20function%20GetAuthToken%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%2064%20bit%20make%20sure%20to%20change%20the%20version%20to%20the%20one%20available%20on%20your%20machine%20%3CBR%20%2F%3E%20%24adalPath%20%3D%20%22%24%7Benv%3AProgramFiles%7D%5CWindowsPowerShell%5CModules%5CAzureRM.profile%5C5.5.1%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%2032%20bit%20uncomment%20if%20this%20is%20your%20scenario%20%3CBR%20%2F%3E%20%23%20adalPath%20%3D%20%22%24%7Benv%3AProgramFiles(x86)%7D%5CWindowsPowerShell%5CModules%5CAzureRM.profile%5C5.3.4%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24adal%20%3D%20%22%24adalPath%5CMicrosoft.IdentityModel.Clients.ActiveDirectory.dll%22%20%3CBR%20%2F%3E%20%24adalforms%20%3D%20%22%24adalPath%5CMicrosoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFrom(%24adal)%20%7C%20Out-Null%20%3CBR%20%2F%3E%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFrom(%24adalforms)%20%7C%20Out-Null%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24authContext%20%3D%20New-Object%20%22Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext%22%20-ArgumentList%20%24authorityUri%20%3CBR%20%2F%3E%20%24authResult%20%3D%20%24authContext.AcquireToken(%24resourceUri%2C%20%24clientId%2C%20%24redirectUri%2C%20%22Always%22)%20%23%20Not%20sure%20when%20to%20use%20AcquireToken()%20or%20AcquireTokenAsync()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20%24authResult%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Call%20GetAuthToken%20to%20acquire%20token%20based%20on%20credentials%20entered%20in%20prompt%20%3CBR%20%2F%3E%20%24authResult%20%3D%20GetAuthToken%20%3CBR%20%2F%3E%20%24authResult.AccessToken%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Server%20name%2C%20database%20name%20and%20the%20connection%20string%20that%20will%20be%20used%20to%20open%20connection%20%3CBR%20%2F%3E%20%24sqlServerUrl%20%3D%20%22fcobo.database.windows.net%22%20%3CBR%20%2F%3E%20%24database%20%3D%20%22fcobo%22%20%3CBR%20%2F%3E%20%24connectionString%20%3D%20%22Server%3Dtcp%3A%24sqlServerUrl%2C1433%3BInitial%20Catalog%3D%24database%3BPersist%20Security%20Info%3DFalse%3BMultipleActiveResultSets%3DFalse%3BEncrypt%3DTrue%3BTrustServerCertificate%3DFalse%3B%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Create%20the%20connection%20object%20%3CBR%20%2F%3E%20%24connection%20%3D%20New-Object%20System.Data.SqlClient.SqlConnection(%24connectionString)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Set%20AAD%20generated%20token%20to%20SQL%20connection%20token%20%3CBR%20%2F%3E%20%24connection.AccessToken%20%3D%20%24authResult.AccessToken%20%3CBR%20%2F%3E%20%24connection.AccessToken%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20prints%20the%20connection%20information%2C%20it's%20still%20closed%20at%20this%20point%2C%20will%20open%20later%20on.%20The%20token%20is%20already%20attached.%20%3CBR%20%2F%3E%20%24connection%20%23%20Query%20that%20will%20be%20sent%20when%20the%20connection%20is%20open.%20I%20had%20a%204%2C000%20record%20table%20and%20I%20was%20able%20to%20truncate%20with%20this%20script%20%24query%20%3D%20%22TRUNCATE%20TABLE%20TEST1%22%20%23%20Opens%20connection%20to%20Azure%20SQL%20Database%20and%20executes%20a%20query%20%24connection.Open()%20%23%20After%20this%2C%20the%20token%20is%20no%20longer%20there%2C%20I%20believe%20this%20is%20because%20the%20authentication%20went%20through%20already%2C%20so%20it%20gets%20rid%20of%20it.%20%3CBR%20%2F%3E%20%24connection%20%3CBR%20%2F%3E%20%24command%20%3D%20New-Object%20-Type%20System.Data.SqlClient.SqlCommand(%24query%2C%20%24connection)%20%3CBR%20%2F%3E%20%24command.ExecuteNonQuery()%20%3CBR%20%2F%3E%20%24connection.Close()%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3C%2FEM%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-369078%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Oct%2026%2C%202018%20How%20to%20connect%20to%20Azure%20SQL%20Database%20using%20token-based%20authentication%20in%20PowerShell%20native%20appsThis%20guide%20assumes%20you%20already%20have%20a%20deployment%20of%20an%20Azure%20SQL%20Database%2C%20your%20PowerShell%20environment%20configured%20and%20you%20have%20an%20app%20registration%20for%20a%20native%20app%20in%20Azure%20Active%20Directory.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-369078%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAAD%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eaad%20token%20based%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ecode%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ecoding%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Etoken%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Etoken%20based%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Oct 26, 2018
How to connect to Azure SQL Database using token-based authentication in PowerShell native apps
This guide assumes you already have a deployment of an Azure SQL Database, your PowerShell environment configured and you have an app registration for a native app in Azure Active Directory.

How to configure a native app registration: https://blogs.msdn.microsoft.com/azuresqldbsupport/2018/10/30/how-to-create-a-native-powershell...


Problem summary:
There might come a time when you want to connect to your database with token-based authentication for some business need or for automation purposes.

Explanation of this guide:
This guide will explain how to connect to Azure SQL Database using token-based authentication in PowerShell using Native application registrations.

Tenant Name
First, we need to determine what our AAD Directory ID is. For that, please go to your Azure Active Directory blade and go to Properties.

[code language="powershell"]
$tenantName = "{AAD DIRECTORY ID}" # Tenant Name this is found in AAD > Properties > Directory ID
[/code]

Application ID
Now we need to determine what our Application ID is and for that we also need to create a new App Registration and provide the proper permissions.
In your Azure Active Directory, go to App Registrations and create a new +New application registration.
Please capture the Application ID field and change it for the {APPLICATION ID} in the code snippet.

[code language="powershell"]
$clientId = "{APPLICATION ID}" # Application ID is found in AAD > App Registrations > Application ID
[/code]

Redirection URI and Resource URI
The redirection URI is determined from your portal, if it's a native Application I'd suggest you to put https://localhost/.

[code language="powershell"]
# Rediret URI is found in AAD > App Registrations > Settings > Redirect URIs</span>

$redirectUri  = "{REDIRECT URI}"

# Resource URI is the resource that you are trying to connect to.
# Resource URI > Found in AAD > App Registrations > Settings > Required Permissions ></span>
# Add > Search Bar *Type in* > Azure SQL DB > Add Rights > OK >
# OK > Grant permissions

$resourceUri  = "https://database.windows.net/"
[/code]

Authority URI
This is the STS link that we will use to authenticate our Tenant.

[code language="powershell"]
$authorityUri = "https://login.microsoftonline.com/$tenantName" # Authority URI https://login.microsoftonline.com/{DirectoryId|TenantName}
[/code]

Script

[code language="powershell"]
$tenantName = "{AAD DIRECTORY ID}" # Tenant Name this is found in AAD > Properties > Directory ID
$clientId = "{APPLICATION ID}" # Application ID is found in AAD > App Registrations > Application ID
$redirectUri = "{REDIRECT URI}" # Rediret URI is found in AAD > App Registrations > Settings > Redirect URIs
$resourceUri = "https://database.windows.net/" # Resource URI > Found in AAD > App Registrations > Settings > Required Permissions >
# Add > Search Bar *Type in* > Azure SQL DB > Add Rights > OK > OK > Grant permissions
$authorityUri = "https://login.microsoftonline.com/$tenantName" # Authority URI https://login.microsoftonline.com/{DirectoryId|TenantName}

# Credit to Ray Held for this function, great stuff!
function GetAuthToken {

# 64 bit make sure to change the version to the one available on your machine
$adalPath = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.5.1"

# 32 bit uncomment if this is your scenario
# adalPath = "${env:ProgramFiles(x86)}\WindowsPowerShell\Modules\AzureRM.profile\5.3.4"

$adal = "$adalPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "$adalPath\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authorityUri
$authResult = $authContext.AcquireToken($resourceUri, $clientId, $redirectUri, "Always") # Not sure when to use AcquireToken() or AcquireTokenAsync()

return $authResult
}

# Call GetAuthToken to acquire token based on credentials entered in prompt
$authResult = GetAuthToken
$authResult.AccessToken

# Server name, database name and the connection string that will be used to open connection
$sqlServerUrl = "fcobo.database.windows.net"
$database = "fcobo"
$connectionString = "Server=tcp:$sqlServerUrl,1433;Initial Catalog=$database;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;"

# Create the connection object
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)

# Set AAD generated token to SQL connection token
$connection.AccessToken = $authResult.AccessToken
$connection.AccessToken

# prints the connection information, it's still closed at this point, will open later on. The token is already attached.
$connection

# Query that will be sent when the connection is open. I had a 4,000 record table and I was able to truncate with this script
$query = "TRUNCATE TABLE TEST1"

# Opens connection to Azure SQL Database and executes a query
$connection.Open()
# After this, the token is no longer there, I believe this is because the authentication went through already, so it gets rid of it.
$connection
$command = New-Object -Type System.Data.SqlClient.SqlCommand($query, $connection)
$command.ExecuteNonQuery()
$connection.Close()
[/code]