嵌入 Power BI embedded 会出现 406 Not Acceptable 错误
Embedding Power BI embedded gives a 406 Not Acceptable error
当我们尝试在现有应用程序中嵌入“Power BI embedded”时,遇到了 406 不接受错误。
为了确保这不是由我们自己的应用程序引起的,我们使用了 power bi 示例存储库中的示例代码:https://github.com/Microsoft/PowerBI-Developer-Samples。我们使用了 "App Owns Data" 方案,因为我们的最终用户不拥有 Power BI Pro 许可证。我们遵循了所有必要的配置步骤,并在 web.config.
中提供了必要的 credentials/id
在我们第一次尝试 运行 应用程序时,我们收到连接关闭错误。经过一番研究,我们发现这是由不正确的 TLS 版本引起的。将 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
添加到控制器解决了这个问题。
我们遇到的下一个错误是在以下代码行:
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, credential);
此行以 http 异常结束:406 - 不可接受。当我们使用 Fiddler 检查流量时,这个错误被认为是合乎逻辑的,因为我们的 Azure AD 将我们的 ADFS 服务器称为依赖方,接受 header 内容 JSON,而我们的 ADFS 返回 xml body。
我们的结论是 AcquireTokenAsync 不能与我们公司的 Azure AD/ADFS 环境一起正常工作。为了对此进行调查,我们 运行 进行了多项测试:
我们尝试使用 MSAL 库,而不是使用标准 ADAL 库。然而,这导致了同样的错误。
使用以下代码向 Azure AD 发出(原始)post 请求以获取身份验证令牌:
<pre>
try
{
client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
_result = await client.PostAsync(
new Uri("<a href="https://login.windows.net/common/oauth2/token" rel="nofollow noreferrer">https://login.windows.net/common/oauth2/token</a>"), new FormUrlEncodedContent(
new[]
{
new KeyValuePair("resource", "<a href="https://analysis.windows.net/powerbi/api" rel="nofollow noreferrer">https://analysis.windows.net/powerbi/api</a>"),
new KeyValuePair("client_id", ClientId),
new KeyValuePair("grant_type", "password"),
new KeyValuePair("username", UserName),
new KeyValuePair("password", Password),
new KeyValuePair("scope", "openid"),
}));
}
catch (HttpOperationException ex)
{
//Bad Request
var content = ex.Response.Content;
Console.WriteLine(content);
}
This resulted in the following error, which we were not able to solve:
{"error":"invalid_grant","error_description":"AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password\r\nTrace ID: b8a97eae-63a4-4d56-8afd-e18eb7b02800\r\nCorrelation ID: 3e168d8f-61ab-4b7f-b9c4-6ae7870c5e06\r\nTimestamp: 2018-12-03 12:59:38Z","error_codes":[70002,50126],"timestamp":"2018-12-03 12:59:38Z","trace_id":"b8a97eae-63a4-4d56-8afd-e18eb7b02800","correlation_id":"3e168d8f-61ab-4b7f-b9c4-6ae7870c5e06"}
We performed an interactive logon by using the following code with success:
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, new Uri("http://localhost:42734/"), new PlatformParameters(PromptBehavior.Auto));
然而,这并不理想,因为我们不希望我们的 end-users 每次使用我们的应用程序中嵌入的 Power BI 时(交互地)使用我们 AD 中的代理帐户登录:)
- 关注微软关于这种情况的博客也没有结果:
https://blogs.msdn.microsoft.com/azuredev/2018/01/22/accessing-the-power-bi-apis-in-a-federated-azure-ad-setup/
问题是如何解决这个问题....请参阅下面我们经过数月 (!) 搜索后找到的答案。
经过几个月的搜索,我在地球另一端的一位同事帮助了我。解决方案很简单:使用在 Azure AD 中创建但在依赖 AD 中未知的代理帐户。这样,身份验证将在 Azure AD 中进行,并且不使用依赖的 ADFS。这可以防止我们遇到的所有错误,并将启用 ADAL 和 MSAL 身份验证。
当我们开始将 Power BI 嵌入到我们自己的 .NET Core 应用程序中时,我们发现 post 请求是目前最好的选择,因为我们找不到合适的 username/password 身份验证方法.NET Core 库。 post 请求也适用于前面提到的解决方案。
我希望这个提示可以节省一些人花费我们的时间...
当我们尝试在现有应用程序中嵌入“Power BI embedded”时,遇到了 406 不接受错误。 为了确保这不是由我们自己的应用程序引起的,我们使用了 power bi 示例存储库中的示例代码:https://github.com/Microsoft/PowerBI-Developer-Samples。我们使用了 "App Owns Data" 方案,因为我们的最终用户不拥有 Power BI Pro 许可证。我们遵循了所有必要的配置步骤,并在 web.config.
中提供了必要的 credentials/id在我们第一次尝试 运行 应用程序时,我们收到连接关闭错误。经过一番研究,我们发现这是由不正确的 TLS 版本引起的。将 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
添加到控制器解决了这个问题。
我们遇到的下一个错误是在以下代码行:
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, credential);
此行以 http 异常结束:406 - 不可接受。当我们使用 Fiddler 检查流量时,这个错误被认为是合乎逻辑的,因为我们的 Azure AD 将我们的 ADFS 服务器称为依赖方,接受 header 内容 JSON,而我们的 ADFS 返回 xml body。 我们的结论是 AcquireTokenAsync 不能与我们公司的 Azure AD/ADFS 环境一起正常工作。为了对此进行调查,我们 运行 进行了多项测试:
我们尝试使用 MSAL 库,而不是使用标准 ADAL 库。然而,这导致了同样的错误。
使用以下代码向 Azure AD 发出(原始)post 请求以获取身份验证令牌:
<pre> try { client.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); _result = await client.PostAsync( new Uri("<a href="https://login.windows.net/common/oauth2/token" rel="nofollow noreferrer">https://login.windows.net/common/oauth2/token</a>"), new FormUrlEncodedContent( new[] { new KeyValuePair("resource", "<a href="https://analysis.windows.net/powerbi/api" rel="nofollow noreferrer">https://analysis.windows.net/powerbi/api</a>"), new KeyValuePair("client_id", ClientId), new KeyValuePair("grant_type", "password"), new KeyValuePair("username", UserName), new KeyValuePair("password", Password), new KeyValuePair("scope", "openid"), })); } catch (HttpOperationException ex) { //Bad Request var content = ex.Response.Content; Console.WriteLine(content); }
This resulted in the following error, which we were not able to solve:
{"error":"invalid_grant","error_description":"AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password\r\nTrace ID: b8a97eae-63a4-4d56-8afd-e18eb7b02800\r\nCorrelation ID: 3e168d8f-61ab-4b7f-b9c4-6ae7870c5e06\r\nTimestamp: 2018-12-03 12:59:38Z","error_codes":[70002,50126],"timestamp":"2018-12-03 12:59:38Z","trace_id":"b8a97eae-63a4-4d56-8afd-e18eb7b02800","correlation_id":"3e168d8f-61ab-4b7f-b9c4-6ae7870c5e06"}
We performed an interactive logon by using the following code with success:
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, new Uri("http://localhost:42734/"), new PlatformParameters(PromptBehavior.Auto));
然而,这并不理想,因为我们不希望我们的 end-users 每次使用我们的应用程序中嵌入的 Power BI 时(交互地)使用我们 AD 中的代理帐户登录:)
- 关注微软关于这种情况的博客也没有结果: https://blogs.msdn.microsoft.com/azuredev/2018/01/22/accessing-the-power-bi-apis-in-a-federated-azure-ad-setup/
问题是如何解决这个问题....请参阅下面我们经过数月 (!) 搜索后找到的答案。
经过几个月的搜索,我在地球另一端的一位同事帮助了我。解决方案很简单:使用在 Azure AD 中创建但在依赖 AD 中未知的代理帐户。这样,身份验证将在 Azure AD 中进行,并且不使用依赖的 ADFS。这可以防止我们遇到的所有错误,并将启用 ADAL 和 MSAL 身份验证。
当我们开始将 Power BI 嵌入到我们自己的 .NET Core 应用程序中时,我们发现 post 请求是目前最好的选择,因为我们找不到合适的 username/password 身份验证方法.NET Core 库。 post 请求也适用于前面提到的解决方案。
我希望这个提示可以节省一些人花费我们的时间...