我有一个 mvc 应用程序,它有一个主页视图,在主页视图中我有按钮来查看报告,它正在加载两个部分视图,一个是获取 ajax 调用并从表中加载外部 api 数据,另一个部分视图是用于负载 powerbi。
在单击按钮时,我加载了不同的 api 并在 1 个局部视图中获取数据,并在加载 PowerBi 的第二个局部视图时,它重定向到 azure 登录并破坏了应用程序。
public ActionResult Index(){
var @params = new NameValueCollection
{
{"response_type", "code"},
{"client_id", Properties.Settings.Default.ClientID},
{"resource", "https://analysis.windows.net/powerbi/api"},
{"redirect_uri", "http://localhost:13526/Redirect"}
};
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString.Add(@params);
string authorityUri = "https://login.windows.net/common/oauth2/authorize/";
var authUri = String.Format("{0}?{1}", authorityUri, queryString);
Response.Redirect(authUri);
}
身份验证后,Azure 颁发令牌并加载 powerbi。
嵌入PowerBi
public async Task<ActionResult> EmbedPowerBi ()
{
AuthenticationResult authResult;
authResult = (AuthenticationResult)Session["authResult"];
var token = authResult.AccessToken;
ViewBag.Token = token;
var tokenCredentials = new TokenCredentials(token, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of dashboards.
var dashboards = await client.Dashboards.GetDashboardsInGroupAsync(GroupId);
// Get the first report in the group.
var dashboard = dashboards.Value.FirstOrDefault();
//var dashboard = dashboards.Value.Where(w => w.Id == "DashboardId");
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Dashboards.GenerateTokenInGroupAsync(GroupId, dashboard.Id, generateTokenRequestParameters);
if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}
// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = dashboard.EmbedUrl,
Id = dashboard.Id
};
return View(embedConfig);
}
}
我遇到的问题是由于重定向其他部分视图会影响当前应用程序。
在主视图中,我正在加载部分视图。
@Html.Partial("loadProduct"); @Html.Partial("_EmbedPowerBi");