gpt4 book ai didi

c# - PowerBI 和 Azure AD headless 登录

转载 作者:行者123 更新时间:2023-12-03 04:26:23 25 4
gpt4 key购买 nike

我正在尝试将 PowerBI 仪表板嵌入到我的客户 MVC 门户中。我的客户没有AAD帐户,因此他们访问网站时无法登录Live,他们以个人权限登录我的MVC网站。

我已在 PowerBI/AAD 上注册了我的应用程序并拥有 ClientID 和 Secret。我调用 AAD 并获取授权代码,然后使用该代码获取身份验证 token ,并成功返回该 token 。

每当我使用访问 token 获取仪表板时,它都会不断被拒绝,并显示 403 Forbidden。

我已经浏览了 Microsoft 的所有示例,但它们需要用户登录提示。我已查看了引用 AcquireToken 方法的 ADAL2.0 代码,但这在 ADAL3 中已弃用,并替换为具有不同参数的 AcquireTokenAsync,我在下面的示例中使用了该方法。

这是获取 token 的函数:

    protected AuthenticationResult GetAccessToken()
{
string pBiUser = Properties.Settings.Default.PowerBIUser;
string pBiPwd = Properties.Settings.Default.PowerBIPwd;
string pBiClientId = Properties.Settings.Default.PowerBIClientId;
string pBiSecret = Properties.Settings.Default.PowerBIClientSecret;
TokenCache TC = new TokenCache();
ClientCredential CC = new ClientCredential(pBiClientId,pBiSecret);
string AU = Properties.Settings.Default.PowerBIAuthority;
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authenticationContext
= new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(AU, TC);
AuthenticationResult result = authenticationContext.AcquireTokenAsync("https://analysis.windows.net/powerbi/api"
,CC).Result;

if (result == null)
{
throw new InvalidOperationException("Failed to obtain the PowerBI token");
}

return result;
}

然后我获取结果 token 并调用。 响应收到 403:

    protected PBIDashboards GetDashboards(AuthenticationResult authResult)
{
PBIDashboards pbiDashboards = new PBIDashboards();
var baseAddress = new Uri("https://api.powerbi.com");
using (var httpClient = new System.Net.Http.HttpClient {BaseAddress = baseAddress})
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization",
"Bearer " + authResult.AccessToken);
using (**var response** = httpClient.GetAsync("v1.0/myorg/dashboards").Result)
{
string responseData = response.Content.ReadAsStringAsync().Result;

//Deserialize JSON string
pbiDashboards = JsonConvert.DeserializeObject<PBIDashboards>(responseData);

if (pbiDashboards != null)
{
var gridViewDashboards = pbiDashboards.value.Select(dashboard => new
{
Id = dashboard.id,
DisplayName = dashboard.displayName,
EmbedUrl = dashboard.embedUrl
});
}
}
}
return pbiDashboards;
}

最佳答案

根据错误消息(403),问题与权限有关。

据我所知,当我们使用 Power BI REST 的客户端凭据流获取访问 token 时,我们无法使用此类权限。您可以引用下图的权限:

enter image description here

要在无需用户交互的情况下获取 Power BI REST 的 token ,我们可以使用资源所有者密码凭据流程。并且您可以使用第3方库PowerBI.Api.Client已经实现了这个。

关于c# - PowerBI 和 Azure AD headless 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com