【问题标题】:How to AcquireToken with PromptBehavior in the latest Microsoft.IdentityModel.Clients.ActiveDirectory如何在最新的 Microsoft.IdentityModel.Clients.ActiveDirectory 中使用 PromptBehavior 获取令牌
【发布时间】:2016-10-27 01:01:10
【问题描述】:

在旧版本的 Microsoft.IdentityModel.Clients.ActiveDirectory 中有带有 PromptBehavior 参数的 AcquireToken

var context = new AuthenticationContext("https://login.windows.net/tenantId");
var result = context.AcquireToken(clientId: clientIdValue, redirectUri: new Uri("http://localhost/Appcycle"), resource: "https://management.core.windows.net/", promptBehavior: PromptBehavior.Auto);

在 Microsoft.IdentityModel.Clients.ActiveDirectory v3.10 中只有 AcquireTokenAsync

var authParam = new PlatformParameters(PromptBehavior.Auto,false);
var result = context.AcquireTokenAsync("https://management.core.windows.net/", clientid, new Uri("http://localhost/AppPoolRecycle"), authParam);
result.Wait();

当我运行这个我得到错误 {"无效的所有者窗口类型。预期类型为 IWin32Window 或 IntPtr(用于窗口句柄)。"}

不确定这是否是因为我在控制台应用程序上运行。如果是这样,我该如何让它工作?

【问题讨论】:

    标签: azure azure-active-directory azure-automation


    【解决方案1】:

    您收到此错误的原因是因为您在 PlatformParameters 构造函数中为第二个参数传入“false”。

    在最新版本的 ADAL (Microsoft.IdentityModel.Clients.ActiveDirectory v3.10) 中,第二个参数是(来自https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/blob/7c9091a0edecf401fea402275e4a64aca95e40fe/src/ADAL.PCL.Desktop/PlatformParameters.cs):

        /// <summary>
        /// Gets the owner of the browser dialog which pops up for receiving user credentials. It can be null.
        /// </summary>
        public object OwnerWindow { get; private set; }
    

    你传入的是 false,它在编译时被接受,因为它是一个对象,但在运行时不被接受,因为它不是一个窗口。

    要解决此问题,请不要传入此参数或将其作为 null 传入。这将使您的控制台应用程序启动一个提示用户登录的窗口。

    如果这是一个控制台应用程序,它应该在没有任何用户交互的情况下运行,那么您应该通过 AcquireTokenAsync 的其他重载来使用仅限应用程序的流程:

        /// <summary>
        /// Acquires security token from the authority.
        /// </summary>
        /// <param name="resource">Identifier of the target resource that is the recipient of the requested token.</param>
        /// <param name="clientCredential">The client credential to use for token acquisition.</param>
        /// <returns>It contains Access Token and the Access Token's expiration time. Refresh Token property will be null for this overload.</returns>        
        public async Task<AuthenticationResult> AcquireTokenAsync(string resource, ClientCredential clientCredential)
    

    【讨论】:

    • 感谢您的帮助。我计划在没有任何用户交互的情况下运行。 ClientCredential 只接受 ClientId 和 ClientSecret。根据我的阅读,ClientSecret 仅适用于 Web 应用程序,不适用于控制台应用程序的“本机客户端应用程序”。如果我错了,请纠正我
    • 你说得对,我们正在开发原生 WPF 应用程序,需要用户交互,因为我们没有秘密。
    猜你喜欢
    • 2020-10-16
    • 2021-01-29
    • 2018-11-26
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多