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

使用C#通过SPN和密码获取Azure Analysis Service角色遇错求助

获取Azure Analysis Services数据库角色的C#替代方案

针对你遇到的Microsoft.AnalysisServices.retail.amd64包在.NET 6下的兼容性问题,以下是几种可行的C#解决方案:

1. 使用适配.NET 6+的官方客户端包

替换原有的.NET Framework专属包,改用Microsoft.AnalysisServices.Core——这是官方推出的支持.NET Standard/.NET Core/.NET 6+的版本,完全兼容现代.NET环境。

代码示例

using Microsoft.AnalysisServices;

// 初始化服务器连接
var server = new Server();
// 连接字符串根据认证方式调整,以下是Azure AD用户密码认证示例
string connectionString = "Provider=MSOLAP;Data Source=asazure://westus.asazure.windows.net/your-as-server;User ID=your-user@domain.com;Password=your-password;Persist Security Info=True;Impersonation Level=Impersonate";

server.Connect(connectionString);

// 目标数据库
var targetDb = server.Databases["YourDatabaseName"];

// 遍历并输出角色信息
foreach (Role role in targetDb.Roles)
{
    Console.WriteLine($"角色名称: {role.Name}");
    Console.WriteLine($"角色ID: {role.ID}");
    // 可扩展获取角色成员、权限等详细信息
}

server.Disconnect();

如果使用Azure AD服务主体认证,连接字符串可替换为:

Provider=MSOLAP;Data Source=asazure://westus.asazure.windows.net/your-as-server;User ID=app:{client-id}@{tenant-id};Password={client-secret};Persist Security Info=True;Impersonation Level=Impersonate

2. 调用Azure Analysis Services REST API

无需依赖任何AS客户端库,直接通过REST API获取角色信息,完全适配.NET 6+,适合云原生场景。

代码示例(使用Azure.Identity获取令牌)

using Azure.Identity;
using System.Net.Http.Headers;
using System.Text.Json;

var serverHost = "your-as-server.asazure.windows.net";
var databaseName = "YourDatabaseName";
var tenantId = "your-tenant-id";
var clientId = "your-service-principal-client-id";
var clientSecret = "your-service-principal-secret";

// 获取Azure AD访问令牌
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var tokenResult = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { "https://*.asazure.windows.net/.default" }));

// 调用REST API
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.Token);
var apiUrl = $"https://{serverHost}/v1/databases/{databaseName}/roles";

var response = await httpClient.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();

var rolesJson = await response.Content.ReadAsStringAsync();
var roles = JsonSerializer.Deserialize<List<dynamic>>(rolesJson);

// 输出角色信息
foreach (var role in roles)
{
    Console.WriteLine($"角色名称: {role.name}");
    Console.WriteLine($"角色ID: {role.id}");
}

3. 临时切换到.NET Framework(仅应急)

如果必须使用原有的Microsoft.AnalysisServices.retail.amd64包,可将项目目标框架改为.NET Framework 4.7.2或更高版本,这样就能绕过兼容性警告和运行时错误。但此方案不推荐,因为.NET Framework在云部署、性能和维护上都不如现代.NET。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:31:12