将 power bi 嵌入 asp net core 2 mvc 的身份验证
authentication to embed powerbi into aspnetcore 2 mvc
这里有一个关于如何嵌入 powerbi App Owns Data 的优秀示例:
https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-embed-sample-app-owns-data/
但是这个例子是针对 .net 平台上的 运行
示例中使用的一些例程对于 aspnetcore
不存在
关于有人试图做同样的事情,还有另一个很棒的 post:
还有这个:
https://community.powerbi.com/t5/Developer/Embed-Power-BI-dashboard-in-ASP-Net-core/td-p/273279
但是我什至无法验证这些东西
这是我当前的代码:
private static readonly string AuthorityUrl = "https://login.windows.net/common/oauth2/authorize/";
private static readonly string ResourceUrl = "https://analysis.windows.net/powerbi/api";
private static readonly string ApiUrl = "https://api.powerbi.com/";
private static readonly string EmbedUrl = "https://app.powerbi.com/";
private static readonly string ClientId = "xxxxclientid";
private static readonly string Username = "xxxxusername";
private static readonly string Password = "xxxxpass";
private static readonly string GroupId = "xxxxgroup";
private static readonly string ReportId = "xxxxreportid";
public async Task<ActionResult> embed(string username, string roles)
{
var result = new EmbedConfig();
try
{
result = new EmbedConfig { Username = username, Roles = roles };
var error = GetWebConfigErrors();
if (error != null)
{
result.ErrorMessage = error;
return View(result);
}
var authenticationResult = await AuthenticateAsync();
if (authenticationResult == null)
{
result.ErrorMessage = "Authentication Failed.";
return View(result);
}
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
... [the rest is post-authentication stuff, ie powerbi]
}
private static async Task<OAuthResult> AuthenticateAsync()
{
Uri oauthEndpoint = new Uri(AuthorityUrl);
using (HttpClient client = new HttpClient()) {
HttpResponseMessage result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("resource", ResourceUrl),
new KeyValuePair<string, string>("client_id", ClientId),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Username),
new KeyValuePair<string, string>("password", Password),
new KeyValuePair<string, string>("scope", "openid"),
}));
string content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<OAuthResult>(content);
}
}
其中 oAuthResult class 是
public class OAuthResult
{
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("scope")]
public string Scope { get; set; }
[JsonProperty("experies_in")]
public int ExpiresIn { get; set; }
[JsonProperty("ext_experies_in")]
public int ExtExpiresIn { get; set; }
[JsonProperty("experies_on")]
public int ExpiresOn { get; set; }
[JsonProperty("not_before")]
public int NotBefore { get; set; }
[JsonProperty("resource")]
public Uri Resource { get; set; }
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
}
我知道我的凭据有效
因为我用它们classic .net 构建了原始解决方案
他们工作得很顺利
但是现在在这个版本中,我在 AuthenticateAsync 中收到了这个响应(只有一部分):
"\r\n\r\n\r\n\r\n\r\n 登录您的 account\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n\r\n \r\n https://login.windows.net/common/jsdisabled\" />\r\n \r\n https ://secure.aadcdn.microsoftonline-p.com/ests/2.1.7382.8/content/images/favicon_a.ico\" />\r\n \r\n "
这显然不是 JSON 响应,而是 ms 登录框的 html
事实上,我设法渲染了输出,它看起来像这样
任何人都可以指出我正确的方向以便
它正确验证并向我发送令牌而不是登录框?
谢谢
更新:
我试过直接通过浏览器登录url
这是 url 我使用的是:
https://login.windows.net/common/oauth2/authorize/?client_id=myid&grant_type=password&username=myun&password=mypw&scope=openid
对于非敏感信息,url 至少看起来正确吗?
仍在获取登录框
你应该使用:AuthorityUrl = "https://login.windows.net/common/oauth2/token/"
请参阅 -
这里有一个关于如何嵌入 powerbi App Owns Data 的优秀示例:
https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-embed-sample-app-owns-data/
但是这个例子是针对 .net 平台上的 运行
示例中使用的一些例程对于 aspnetcore
关于有人试图做同样的事情,还有另一个很棒的 post:
还有这个:
https://community.powerbi.com/t5/Developer/Embed-Power-BI-dashboard-in-ASP-Net-core/td-p/273279
但是我什至无法验证这些东西
这是我当前的代码:
private static readonly string AuthorityUrl = "https://login.windows.net/common/oauth2/authorize/";
private static readonly string ResourceUrl = "https://analysis.windows.net/powerbi/api";
private static readonly string ApiUrl = "https://api.powerbi.com/";
private static readonly string EmbedUrl = "https://app.powerbi.com/";
private static readonly string ClientId = "xxxxclientid";
private static readonly string Username = "xxxxusername";
private static readonly string Password = "xxxxpass";
private static readonly string GroupId = "xxxxgroup";
private static readonly string ReportId = "xxxxreportid";
public async Task<ActionResult> embed(string username, string roles)
{
var result = new EmbedConfig();
try
{
result = new EmbedConfig { Username = username, Roles = roles };
var error = GetWebConfigErrors();
if (error != null)
{
result.ErrorMessage = error;
return View(result);
}
var authenticationResult = await AuthenticateAsync();
if (authenticationResult == null)
{
result.ErrorMessage = "Authentication Failed.";
return View(result);
}
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
... [the rest is post-authentication stuff, ie powerbi]
}
private static async Task<OAuthResult> AuthenticateAsync()
{
Uri oauthEndpoint = new Uri(AuthorityUrl);
using (HttpClient client = new HttpClient()) {
HttpResponseMessage result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("resource", ResourceUrl),
new KeyValuePair<string, string>("client_id", ClientId),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Username),
new KeyValuePair<string, string>("password", Password),
new KeyValuePair<string, string>("scope", "openid"),
}));
string content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<OAuthResult>(content);
}
}
其中 oAuthResult class 是
public class OAuthResult
{
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("scope")]
public string Scope { get; set; }
[JsonProperty("experies_in")]
public int ExpiresIn { get; set; }
[JsonProperty("ext_experies_in")]
public int ExtExpiresIn { get; set; }
[JsonProperty("experies_on")]
public int ExpiresOn { get; set; }
[JsonProperty("not_before")]
public int NotBefore { get; set; }
[JsonProperty("resource")]
public Uri Resource { get; set; }
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
}
我知道我的凭据有效
因为我用它们classic .net 构建了原始解决方案
他们工作得很顺利
但是现在在这个版本中,我在 AuthenticateAsync 中收到了这个响应(只有一部分):
"\r\n\r\n\r\n\r\n\r\n 登录您的 account\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n\r\n \r\n https://login.windows.net/common/jsdisabled\" />\r\n \r\n https ://secure.aadcdn.microsoftonline-p.com/ests/2.1.7382.8/content/images/favicon_a.ico\" />\r\n \r\n "
这显然不是 JSON 响应,而是 ms 登录框的 html
事实上,我设法渲染了输出,它看起来像这样
任何人都可以指出我正确的方向以便
它正确验证并向我发送令牌而不是登录框?
谢谢
更新:
我试过直接通过浏览器登录url
这是 url 我使用的是:
https://login.windows.net/common/oauth2/authorize/?client_id=myid&grant_type=password&username=myun&password=mypw&scope=openid
对于非敏感信息,url 至少看起来正确吗?
仍在获取登录框
你应该使用:AuthorityUrl = "https://login.windows.net/common/oauth2/token/"
请参阅 -