首页 > 解决方案 > 从 C# 应用程序查询 Azure Log Analytics

问题描述

过去几天我一直在努力从 Azure Log Analytics 查询自定义日志。我一直在关注微软从https://dev.int.loganalytics.io/documentation/1-Tutorials/Direct-API提供的教程,但我一直收到 403。我将工作区的所有权限授予我的 Azure 应用程序 [ ![ALA 工作区上的 Azure 应用程序权限][1]][1] [1]:https://i.stack.imgur.com/xxT2A.png 这是我用来尝试查询 ALA 的简单应用程序代码工作区

static async Task Main(string[] args)
    {
        
        String tenantId = "??????????????????????????????????";
        String applicationId = "??????????????????????????????????";";
        String applictionSecretKey = "??????????????????????????????????";;
        String token;

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            Dictionary<String, String> requestData = new Dictionary<String, String>();
            requestData.Add("grant_type", "client_credentials");
            requestData.Add("client_id", applicationId);
            requestData.Add("client_secret", applictionSecretKey);
            requestData.Add("resource", "https://api.loganalytics.io/");
            FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);

            var request = await client.PostAsync($"https://login.microsoftonline.com/{tenantId}/oauth2/token", requestBody);
            var response = await request.Content.ReadAsStringAsync();
            token = JsonConvert.DeserializeObject<dynamic>(response).access_token;

        }

        String workspaceId = "??????????????????????????????????";;

        String query = JsonConvert.SerializeObject(new
        {
            query = "ApplicationLog_CL | take 10",
            timespan = "PT12H"
        });
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var postContent = new StringContent(query, Encoding.UTF8, "application/json");
            var response = await client.PostAsync($"https://api.loganalytics.io/v1/workspaces/{workspaceId}/query", postContent);

            HttpContent responseContent = response.Content;
            var content = await response.Content.ReadAsStringAsync();

            Console.WriteLine(content);

        }



        Console.ReadKey();
    }

我不断收到来自 ALA API 的 403 响应。任何线索我在这里缺少什么?

标签: c#azureazure-active-directoryazure-log-analytics

解决方案


根据您提供的教程,我在我的网站上进行了测试,效果很好。

以下是您可以排除故障的一些方法。

1.当您add role进入时Access control,您可以添加名称AIDemoApp类似于教程的AAD注册应用程序。

在此处输入图像描述

并且在 AAD 中添加了 Log Analytics API 权限。 在此处输入图像描述

2.new MediaTypeWithQualityHeaderValue("application/json")更改为new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")

3.在AAD中注册的应用中添加权限后,点击Grant Permission

在此处输入图像描述


推荐阅读