%3CLINGO-SUB%20id%3D%22lingo-sub-1407819%22%20slang%3D%22en-US%22%3EStep%20By%20Step%3A%20How%20to%20use%20SQLPackage%20with%20Access%20Token%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1407819%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EPurpose%3C%2FSTRONG%3E%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESQLPackage%20allows%20you%20to%20authenticate%20with%20Access%20Token%20instead%20of%20providing%20Login%20name%20and%20password.%3C%2FP%3E%0A%3CP%3EThis%20article%20will%20show%20you%20how%20to%20do%20that%20end%20to%20end.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGeneral%20steps%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3COL%3E%0A%3CLI%3ECreate%20App%20Registration%20in%20your%20Azure%20Active%20Directory%20(AAD)%3C%2FLI%3E%0A%3CLI%3ECreate%20user%20for%20the%20Application%20to%20access%20Azure%20SQL%20DB%20and%20grant%20the%20needed%20permissions.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EGenerate%20Access%20token%20for%20your%20Application.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EUse%20the%20Access%20token%20to%20import%20or%20export%20your%20database.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EDetailed%20steps%3A%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3COL%3E%0A%3COL%3E%0A%3CLI%3ECreate%20App%20Registration%20in%20your%20Azure%20Active%20Directory%20(AAD)%3C%2FLI%3E%0A%3COL%3E%0A%3CLI%3EOpen%20Azure%20portal%20and%20access%20you%20Azure%20Active%20Directory%20management%20blade%3C%2FLI%3E%0A%3CLI%3EClick%20on%20App%20Registrations%3C%2FLI%3E%0A%3CLI%3EClick%20on%20New%20Registration%3C%2FLI%3E%0A%3CLI%3EGive%20your%20application%20a%20name%20so%20it%20can%20be%20identified%20afterwards%3C%2FLI%3E%0A%3CLI%3EClick%20on%20%E2%80%9CRegister%E2%80%9D%3C%2FLI%3E%0A%3CLI%3EOnce%20the%20App%20is%20created%20you%20will%20be%20redirected%20to%20the%20App%20blade%3C%2FLI%3E%0A%3CLI%3ENote%20your%20application%20(client)%20ID%20%E2%80%93%20you%20will%20use%20that%20later%3C%2FLI%3E%0A%3CLI%3EClick%20on%20%E2%80%9CEndpoints%E2%80%9D%20at%20the%20top%20and%20note%20the%20%E2%80%9C%3CSTRONG%3EOAuth%202.0%20token%20endpoint%20(v2)%E2%80%9D%3C%2FSTRONG%3E%20url%20%E2%80%93%20we%20will%20use%20this%20later%20as%20well.%3C%2FLI%3E%0A%3CLI%3EClick%20on%20%E2%80%9CCertificate%20%26amp%3B%20Secrets%E2%80%9D%3C%2FLI%3E%0A%3CLI%3EClick%20on%20%E2%80%9CNew%20Client%20Secret%E2%80%9D%3C%2FLI%3E%0A%3CLI%3ESet%20the%20expiry%20time%20and%20click%20%E2%80%9CAdd%E2%80%9D%3C%2FLI%3E%0A%3CLI%3ENote%20the%20value%20of%20the%20key%20%E2%80%93%20we%20will%20use%20it%20later.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CLI%3ECreate%20user%20for%20the%20Application%20to%20access%20Azure%20SQL%20DB%20and%20grant%20the%20needed%20permissions.%26nbsp%3B%3COL%3E%0A%3CLI%3ECREATE%20USER%20%5BSQLAccess%5D%20FROM%20EXTERNAL%20PROVIDER%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3COL%3E%0A%3CLI%3Ealter%20role%20dbmanager%20add%20member%20%5BSQLAccess%5D%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3COL%3E%0A%3CLI%3EMake%20sure%20your%20server%20has%20AAD%20Admin%20account%20configured.%3C%2FLI%3E%0A%3CLI%3EConnect%20to%20you%20SQL%20DB%20with%20your%20AAD%20account%3C%2FLI%3E%0A%3CLI%3ECreate%20the%20user%20for%20the%20application%20access%3C%2FLI%3E%0A%3CLI%3EGrant%20the%20needed%20permissions.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CLI%3EGenerate%20Access%20token%20for%20your%20Application.%26nbsp%3B%3C%2FLI%3E%0A%3COL%3E%0A%3CLI%3EUsing%20PowerShell%20(ADAL.PS%20is%20required)%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Module%20ADAL.PS%20is%20needed%0A%23%20to%20install%20it%20please%20run%20the%20following%20command%0A%23%20Install-Module%20ADAL.PS%0A%0A%24key%3D%20ConvertTo-SecureString%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-String%20%22%7BKey%20Secret%7D%22%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-AsPlainText%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-Force%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%0AGet-AdalToken%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-Resource%20%22https%3A%2F%2Fdatabase.windows.net%2F%22%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-ClientId%20%22%7BApplication%20ID%7D%22%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-ClientSecret%20%24key%20%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-TenantId%20%22%7BTenant%20ID%7D%22%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3COL%3E%0A%3CLI%3EUsing%20C%23%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System%3B%0Ausing%20System.Collections.Generic%3B%0Ausing%20System.Linq%3B%0Ausing%20System.Text%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20Microsoft.IdentityModel.Clients.ActiveDirectory%3B%0A%0Anamespace%20ConsoleApp1%0A%7B%0A%20%20%20%20class%20Program%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20static%20void%20Main(string%5B%5D%20args)%0A%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20clientId%20%3D%20%22%7BClient%20ID%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20aadTenantId%20%3D%20%22%7BTenant%20ID%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20clientSecretKey%20%3D%20%22%7BKey%20Secret%7D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20AadInstance%20%3D%20%22https%3A%2F%2Flogin.windows.net%2F%7B0%7D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20ResourceId%20%3D%20%22https%3A%2F%2Fdatabase.windows.net%2F%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20AuthenticationContext%20authenticationContext%20%3D%20new%20AuthenticationContext(string.Format(AadInstance%2C%20aadTenantId))%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20ClientCredential%20clientCredential%20%3D%20new%20ClientCredential(clientId%2C%20clientSecretKey)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20DateTime%20startTime%20%3D%20DateTime.Now%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%22Time%20%22%20%2B%20String.Format(%22%7B0%3Amm%3Ass.fff%7D%22%2C%20startTime))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20AuthenticationResult%20authenticationResult%20%3D%20authenticationContext.AcquireTokenAsync(ResourceId%2C%20clientCredential).Result%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20DateTime%20endTime%20%3D%20DateTime.Now%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%22Got%20token%20at%20%22%20%2B%20String.Format(%22%7B0%3Amm%3Ass.fff%7D%22%2C%20endTime))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%22Total%20time%20to%20get%20token%20in%20milliseconds%20%22%20%2B%20(endTime%20-%20startTime).TotalMilliseconds)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(authenticationResult.AccessToken.ToString())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.ReadKey()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4.%20Use%20the%20Access%20token%20to%20import%20or%20export%20your%20database.%3C%2FP%3E%0A%3CUL%3E%0A%3COL%3E%0A%3COL%3E%0A%3CLI%3EUse%20your%20SQLPackage%20command%20and%20instead%20of%20using%20Login%20%2F%20User%20and%20password%20use%20the%20%2FAccessToken%3A%7BAccessTokenHere%7D%20(or%20%2Fat)%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E
Microsoft

Purpose

SQLPackage allows you to authenticate with Access Token instead of providing Login name and password.

This article will show you how to do that end to end. 

 

General steps:

      1. Create App Registration in your Azure Active Directory (AAD)
      2. Create user for the Application to access Azure SQL DB and grant the needed permissions. 
      3. Generate Access token for your Application. 
      4. Use the Access token to import or export your database.

 

Detailed steps: 

      1. Create App Registration in your Azure Active Directory (AAD)
        1. Open Azure portal and access you Azure Active Directory management blade
        2. Click on App Registrations
        3. Click on New Registration
        4. Give your application a name so it can be identified afterwards
        5. Click on “Register”
        6. Once the App is created you will be redirected to the App blade
        7. Note your application (client) ID – you will use that later
        8. Click on “Certificate & Secrets”
        9. Click on “New Client Secret”
        10. Set the expiry time and click “Add”
        11. Note the value of the key – we will use it later.
      2. Create user for the Application to access Azure SQL DB and grant the needed permissions. 
        1. CREATE USER [SQLAccess] FROM EXTERNAL PROVIDER
        1. alter role dbmanager add member [SQLAccess]
        1. Make sure your server has AAD Admin account configured.
        2. Connect to you SQL DB with your AAD account
        3. Create the user for the application access
        4. Grant the needed permissions.
      3. Generate Access token for your Application. 
        1. Using PowerShell (ADAL.PS is required)

 

 

 

# Module ADAL.PS is needed
# to install it please run the following command
# Install-Module ADAL.PS

$key= ConvertTo-SecureString `
         -String "{Key Secret}" `
         -AsPlainText `
         -Force
       
Get-AdalToken `
    -Resource "https://database.windows.net/" `
    -ClientId "{Application ID}" `
    -ClientSecret $key `
    -TenantId "{Tenant ID}"

 

 

 

 

      1. Using C#

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            string clientId = "{Client ID}";
            string aadTenantId = "{Tenant ID}";
            string clientSecretKey = "{Key Secret}";

            string AadInstance = "https://login.windows.net/{0}";

            string ResourceId = "https://database.windows.net/";

            AuthenticationContext authenticationContext = new AuthenticationContext(string.Format(AadInstance, aadTenantId));

            ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);
            DateTime startTime = DateTime.Now;
            Console.WriteLine("Time " + String.Format("{0:mm:ss.fff}", startTime));
            AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(ResourceId, clientCredential).Result;
            DateTime endTime = DateTime.Now;
            Console.WriteLine("Got token at " + String.Format("{0:mm:ss.fff}", endTime));
            Console.WriteLine("Total time to get token in milliseconds " + (endTime - startTime).TotalMilliseconds);
            Console.WriteLine(authenticationResult.AccessToken.ToString());
            Console.ReadKey();
        }
    }
}

 

 

 

4. Use the Access token to import or export your database.

      1. Use your SQLPackage command and instead of using Login / User and password use the /AccessToken:{AccessTokenHere} (or /at)