我想将PowerBI仪表板嵌入到我的客户MVC门户中。我的客户没有AAD帐户,所以当他们访问网站时他们无法登录到Live,他们以个人权限登录到我的MVC网站。PowerBI和Azure AD无头登录
我已经在PowerBI/AAD上注册了我的App,并拥有ClientID和Secret。我打电话给AAD并获得一个授权码,然后我用它来获取成功返回的认证令牌。
当我使用访问令牌来获取仪表板时,它会不断被403 Forbidden拒绝。
我已经浏览了微软的所有样本,但他们需要用户登录提示。我已经回顾了引用AcquireToken方法的ADAL2.0代码,但是这在ADAL3中已被弃用,并由具有不同参数的AcquireTokenAsync取代,我在下面的示例中使用了它。
这里获得令牌功能:
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;
}
我再取结果令牌和调用。所述响应接收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;
}
这里的确切问题是什么?您正试图找到一种无需任何用户界面的方式登录用户? –