I have below code to run the kusto query
const string Cluster = <ClusterName>;
const string Database = <Database >;
DefaultAzureCredential credential = new DefaultAzureCredential();
TokenRequestContext tokenRequestContext = new TokenRequestContext(new[] { Cluster });
AccessToken accessToken = await credential.GetTokenAsync(tokenRequestContext);
// Connect to Azure Data Explorer using the managed identity of the web app
KustoConnectionStringBuilder builder = new KustoConnectionStringBuilder($"{Cluster}/{Database}").WithAadApplicationTokenAuthentication(accessToken.Token);
var adminProvider = KustoClientFactory.CreateCslAdminProvider(builder);
IDataReader dataReader = adminProvider.ExecuteControlCommand(Query);
The above snippet taking my user details some how and executiong the query successfully. How to run the kusto query using AAD application. Is it possible to run the query with only application ID, I don’t have secret value and I don’t have admin rights to change the API permissions.
1
How to execute the Kusto query using the AAD application?
To execute the Kusto query with the Azure ad application you need to use .WithAadApplicationKeyAuthentication
in your code.
Code:
using Kusto.Data;
using Kusto.Data.Net.Client;
using static System.Net.WebRequestMethods;
class Program
{
static void Main(string[] args)
{
// Replace placeholders with actual values
string clusterUri = "https://<clustername>.<location>.kusto.windows.net";
string database = "your-database-name";
string applicationId = "your-app-id";
string applicationSecret = "your-app-secret";
string tenantId = "your tenant id";
var kcp = new KustoConnectionStringBuilder(clusterUri, database).WithAadApplicationKeyAuthentication(applicationId, applicationSecret, tenantId);
var kustoClient = KustoClientFactory.CreateCslQueryProvider(kcp);
string query = "table1 | count";
var reader =kustoClient.ExecuteQuery(query);
while (reader.Read())
{
Console.WriteLine(reader.GetInt64(0));
}
}
}
Output:
The above code was executed and successfully returned the count of Table 1
in my database.
1000
Is it possible to run the query with only the application ID, I don’t have a secret value and I don’t have admin rights to change the API permissions.
For AzureADapplication authentication you need ApplicationID,TenantId,Appsecret and also API permissions.
Reference:
Create an Azure AD application in Azure Data Explorer – Azure Data Explorer | Microsoft Learn