c# - 无法授权 Azure LogAnalytics 工作区

标签 c# azure azure-active-directory azure-application-insights azure-log-analytics

enter image description here我正在尝试连接到 Azure 门户中的工作区。我收到的错误为

Operation returned an invalid status code 'Unauthorized'.

creds 对象已获取身份验证 token ,并且我已向我的应用程序添加了资源权限,如本 link 中所述。

using System;
using Microsoft.Azure.OperationalInsights;
using Microsoft.Rest.Azure.Authentication;

namespace LogAnalytics
{
    class Program
    {
        static void Main(string[] args)
        {
            var workspaceId = "**myworkspaceId**";
            var clientId = "**myClientId**";

            var clientSecret = "**myClientSecret**";
            //<your AAD domain>
            var domain = "**myDomain**";
            var authEndpoint = "https://login.microsoftonline.com";
            var tokenAudience = "https://api.loganalytics.io/";

            var adSettings = new ActiveDirectoryServiceSettings
            {
                AuthenticationEndpoint = new Uri(authEndpoint),
                TokenAudience = new Uri(tokenAudience),
                ValidateAuthority = true
            };

            var creds = ApplicationTokenProvider.LoginSilentAsync(domain,clientId, clientSecret, 
                strong textadSettings).GetAwaiter().GetResult();            

            var client = new OperationalInsightsDataClient(creds);
            client.WorkspaceId = workspaceId;

            //Error happens below
            var results = client.Query("union * | take 5");

            Console.WriteLine(results);
            Console.ReadLine();
        }
    }
}

最佳答案

Operation returned an invalid status code 'Unauthorized'.

根据错误消息和您提供的代码,您需要在Azure AD中注册的应用程序中添加权限。

enter image description here

注意:如果您想为应用程序添加权限您需要成为管理员,然后您可以使用ClientIdClientSecret获取身份验证 token 并读取日志分析。

但是,如果您不是管理员,则可以向用户委派权限并使用用户名和密码访问 Azure AD。

要获取用户的身份验证 token ,您可以使用函数 UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password).GetAwaiter().GetResult()获取我们的凭据。

关于c# - 无法授权 Azure LogAnalytics 工作区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604411/

相关文章:

azure - Azure 应用服务可以绑定(bind)到客户端的外部 Azure AD 吗?

c# - Gridview 使用图像排序

c# - 在列表的上限和下限之间找到一个值

c# - 将 async/await 与 void 方法一起使用

azure - AzureWebSites 上的 CORS(同源?)

php - 使用 Microsoft Azure AD 保护应用程序

c# - 深入了解 EF。包含与加入

azure - 如何使用 azure powershell 命令获取给定存储帐户名称和资源组的存储帐户 ID?

azure - az 登录特定订阅

azure - 使用 SSO 时是否可以删除 WAAD 登录页面上的密码字段?