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:
Detailed steps:
# Module MSAL.PS is needed
# to install it please run the following command
# Install-Module MSAL.PS
$key= ConvertTo-SecureString `
-String "{Key Secret}" `
-AsPlainText `
-Force
Get-MsalToken `
-Scope "https://database.windows.net/.default" `
-ClientId "{Application ID}" `
-ClientSecret $key `
-TenantId "{Tenant ID}"
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/.default";
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.
ChangeLog:
2022-04-26: Changed Powershell module from ADAL to MSAL
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.