使用服务主体的 Azure 分析服务连接不起作用

Azure analysis service connection using Service principal not working

我正在尝试使用 ADOMD 连接到 Azure 分析服务并使用服务主体进行身份验证。所以我做了以下工作:

  1. 在 AAD 中创建应用程序。
  2. 已授予应用程序(服务主体)对 Azure 分析服务的读取权限。

下面是我连接到 Azure 分析服务的代码。

            var clientId = "******";
            var clientSecret = "*****";
            var domain = "****.onmicrosoft.com";
            var ssasUrl = "northeurope.asazure.windows.net";
            var token = await TokenHelper.GetAppOnlyAccessToken(domain, $"https://{ssasUrl}", clientId, clientSecret);

            var connectionString = $"Provider=MSOLAP;Data Source=asazure://{ssasUrl}/{modelname};Initial Catalog= adventureworks;User ID=;Password={token};Persist Security Info=True;Impersonation Level=Impersonate";
            var ssasConnection = new AdomdConnection(connectionString);
            ssasConnection.Open();
            var query = @"Evaluate TOPN(10,Customer,Customer[Customer Id],1)";
            var cmd = new AdomdCommand(query)
            {
                Connection = ssasConnection
            };
            using (var reader = cmd.ExecuteXmlReader())
            {
                string value = reader.ReadOuterXml();
                Console.WriteLine(value);
            }

我能够获得有效的访问令牌,但在尝试打开连接时出现以下错误:

AdomdErrorResponseException: 用户 'app:xxxxxxx@xxxxxx' 无权访问 'adventureworks' 数据库,或者数据库不存在。

附加信息:

  1. 我已经验证权限(Reader 并且也尝试过贡献)通过 Azure 门户授予 Azure 分析服务的服务主体。
  2. 我已经用服务帐户(用户名和密码)尝试了相同的代码并且它有效。
  3. 如果我从连接字符串中删除 "Initial Catalog= adventureworks",那么我的连接就会成功。但我不明白为什么分析服务权限没有传播到模型。

解析:

愚蠢的是我在发布后就自己得到了解决方案。上面的第 3 点通过门户给了我对 Azure 分析服务的 clue.Granting 权限不会传播到服务主体(Azuire AD 应用程序)的模型。

步骤:

  1. 在 Sql server Mgmt Studio 中打开 Azure 分析服务。
  2. 在目标模型中,转到角色
  3. 将服务主体添加到具有权限的所需角色中。服务主体以以下格式添加:

    app:[appid]@[tenantid]

示例:app:8249E22B-CFF9-440C-AF27-60064A5743CE@86F119BE-D703-49E2-8B5F-72392615BB97

请参考这个official document

您需要赋予服务主体 OwnerContributor 角色。

您可以在 Azure 门户上完成。 <your subscription>--><Access Control>--><Add>.

有关此的更多信息,请参阅此 official document

愚蠢的是我在发布后就自己解决了这个问题。上面的第 3 点通过门户给了我对 Azure 分析服务的 clue.Granting 权限不会传播到服务主体(Azure AD 应用程序)的模型。

步骤:

  1. 在 Sql server Mgmt Studio 中打开 Azure 分析服务。
  2. 在目标模型中,转到角色
  3. 将服务主体添加到具有权限的所需角色中。服务主体以以下格式添加:

    app:[appid]@[tenantid]

示例:app:8249E22B-CFF9-440C-AF27-60064A5743CE@86F119BE-D703-49E2-8B5F-72392615BB97

我在这里写下了我的全部经历:https://unnieayilliath.com/2017/11/12/connecting-to-azure-analysis-services-using-adomd/