来自 Web 和 Windows 客户端应用程序的带有 ADAL 登录体验的通用 Oauth

Common Oauth with ADAL Login experience from Web and Windows Client App

我正在开发可以通过 Windows Application(Desktop Client)HTML5 (Web Client) 访问的产品。我正在尝试 integrate Power BI reports 到我的产品中。为了访问集成 Power BI 报告所需的 Power BI API,我使用 https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-register-a-client-app/

创建了一个客户端 ID

现在我创建了一个 class 以使用客户端 ID 访问 Power BI API,并使用以下行生成访问令牌:

token = authContext.AcquireToken(resourceUri, ClientID, new Uri(redirectUri),PromptBehavior.RefreshSession).AccessToken;

其中

resourceUri = Uri to PowerBI APIs

redirectUri = https://login.microsoftonline.com/common (Multi-Tenant)

在桌面客户端上,AcquireToken() 方法运行良好,并弹出一个窗口让我输入 AD 登录凭据。但是当从 HTML5 客户端执行时这个方法会失败并抛出异常

An exception of type 'System.InvalidOperationException' occurred in Microsoft.IdentityModel.Clients.ActiveDirectory.dll but was not handled in user code.
Additional information: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

我在一些博客中读到,Oauth 身份验证弹出的登录 window 是基于浏览器的。如果是这样,为什么从 HTML5 客户端调用时它无法弹出?

是否也可以得到类似的弹出式窗口Login window using ADAL & Oauth for HTML5 client

在这方面,我们将不胜感激。

您在桌面上看到的弹出窗口是一个完整的 window 表单 - 显然无法从服务器端网站 运行 的代码隐藏中打开。 您需要注册两个不同的 clientID,一个像您已经注册的那样用于本机客户端(桌面),另一个用于机密客户端(网站)。完成后,您需要驱动 OpenId COnenct 登录 + 访问令牌获取,或者您需要驱动 oauth2 授权请求 - 所有形式的服务器端代码并​​使用浏览器表面作为与用户交互的机制。有关示例,请参见 https://azure.microsoft.com/en-us/documentation/samples/active-directory-dotnet-webapp-webapi-openidconnect/,只需使用您的 Power BI 更改自定义 API。 HTH