You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何构造AadApplicationToken,通过KustoConnectionStringBuilder向Kusto插入数据?

构造AAD应用访问令牌(appAccessToken)的示例

要生成用于Kusto认证的appAccessToken,可以使用Azure AD官方认证库Microsoft.Identity.Client获取令牌,以下是完整的可运行示例:

1. 安装依赖包

先通过NuGet安装所需库,在包管理器控制台执行:

Install-Package Microsoft.Identity.Client
Install-Package Microsoft.Azure.Kusto.Data

2. 完整代码示例

using Microsoft.Identity.Client;
using Kusto.Data;
using Kusto.Data.Net.Client;
using System.Data;

// 替换为你的实际配置参数
string tenantId = "你的Azure AD租户ID";
string clientId = "你的应用注册客户端ID";
string clientSecret = "你的应用注册客户端密钥";
string kustoClusterUri = "https://<你的集群名称>.<区域>.kusto.windows.net";
string kustoResource = "https://kusto.azure.com";

// 使用MSAL获取应用访问令牌
var confidentialClient = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithClientSecret(clientSecret)
    .WithAuthority($"https://login.microsoftonline.com/{tenantId}")
    .Build();

var tokenScopes = new[] { $"{kustoResource}/.default" };
var tokenResult = await confidentialClient.AcquireTokenForClient(tokenScopes).ExecuteAsync();
string appAccessToken = tokenResult.AccessToken;

// 构造Kusto连接并执行数据摄入
var kustoConnStringBuilder = new KustoConnectionStringBuilder(kustoClusterUri)
    .WithAadApplicationTokenAuthentication(appAccessToken);

string ingestCommand = ".ingest inline into table <你的表名> <|\rTesting,200.12.1.0,osname,version";
var adminProvider = KustoClientFactory.CreateCslAdminProvider(kustoConnStringBuilder);

using (IDataReader resultReader = adminProvider.ExecuteControlCommand(ingestCommand))
{
    // 读取摄入操作的返回结果
    while (resultReader.Read())
    {
        Console.WriteLine($"操作状态: {resultReader[0]}");
    }
}

关键注意事项

  • 务必将代码中的占位参数(租户ID、客户端ID等)替换为你的实际信息
  • 需要确保Azure AD应用已被授予目标Kusto集群的Ingestor权限(可在Kusto门户中配置)
  • 示例使用异步方法获取令牌,需将包含代码的方法标记为async

内容的提问来源于stack exchange,提问作者Sravani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:47:40