【发布时间】:2026-02-20 22:50:01
【问题描述】:
这里有一个很好的例子来说明如何嵌入 powerbi App Owns Data:
https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-embed-sample-app-owns-data/
但是这个例子是在 .net 平台上运行的
并且示例中使用的一些例程对于 aspnetcore 不存在
还有一篇关于有人试图做同样事情的精彩帖子:
Embed Power BI Report In ASP.Net Core Website
还有这个:
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 类在哪里
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; }
}
我知道我的凭据有效
因为我用他们在经典 .net 中构建了原始解决方案
他们工作得很好
但是,现在在这个版本中,我在 AuthenticateAsync 中收到了这个响应(只有一部分):
"\r\n\r\n\r\n\r\n\r\n 登录您的帐户\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 直接登录
这是我使用的网址:
https://login.windows.net/common/oauth2/authorize/?client_id=myid&grant_type=password&username=myun&password=mypw&scope=openid
对于非敏感信息,该网址至少看起来正确吗?
仍然得到登录框
【问题讨论】:
标签: asp.net-core-mvc powerbi-embedded