How to authenticate and authorize a user principal to a kusto cluster

Samira Ouraga 5 Reputation points Microsoft Employee
2023-10-12T21:25:45.1033333+00:00

Hello,

I created a Kusto explorer for a feature I am working on. The cluster is active, the database is also active. I want to write a PowerShell script to create tables and columns. What's the best way for user authentication? Is there a script I can use as reference?

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
561 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. QuantumCache 20,356 Reputation points
    2023-10-13T16:49:16.16+00:00

    Hello @Samira Ouraga

    Posting the resolution form the Original Poster (Samira Ouraga)

    I was able to get the token using (Get an access token for a user principal using the Azure CLI) az account set --subscription <SUBSCRIPTION_ID> az account set --name "<SUBSCRIPTION_NAME>" az account get-access-token --resource "https://api.kusto.windows.net" --query "accessToken"

    I hope you came across this document?
    Access control overview

    $tenantId = "your-tenant-id" 
    
    $clientId = "your-client-id" 
    
    $clientSecret = "your-client-secret" 
    
    $resource = "https://kusto.windows.net" 
    
    $authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.microsoftonline.com/$tenantId") 
    
    $credential = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential($clientId, $clientSecret) 
    
    $authResult = $authContext.AcquireTokenAsync($resource, $credential).Result 
    
    $accessToken = $authResult.AccessToken
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.