如何在最新的 Microsoft.IdentityModel.Clients.ActiveDirectory 中使用 PromptBehavior 获取令牌 [英] How to AcquireToken with PromptBehavior in the latest Microsoft.IdentityModel.Clients.ActiveDirectory

查看:14
本文介绍了如何在最新的 Microsoft.IdentityModel.Clients.ActiveDirectory 中使用 PromptBehavior 获取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the older versions of Microsoft.IdentityModel.Clients.ActiveDirectory there is AcquireToken with PromptBehavior parameter

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

In Microsoft.IdentityModel.Clients.ActiveDirectory v3.10 there is only 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(用于窗口句柄)."}

When I run this I get error {"Invalid owner window type. Expected types are IWin32Window or IntPtr (for window handle)."}

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

Not sure if this is due to I am running on a console application. If so how do i get it to work?

推荐答案

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

The reason you're getting this error is because you are passing in "false" for the second parameter in the PlatformParameters constructor.

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

In the latest version of ADAL (Microsoft.IdentityModel.Clients.ActiveDirectory v3.10), this second parameter is (from 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,它在编译时被接受,因为它是一个对象,但在运行时不被接受,因为它不是一个窗口.

You're passing in false, which is accepted at compile time given that it's an object, but not at runtime given that it's not a window.

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

To fix this simply do not pass in this parameter or pass it in as null. This will make your console application launch a window which prompts the user to log in.

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

If this is meant to be a console application that's supposed to run without any user interaction though, then you should either use the app-only flow via this other overload of 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)

这篇关于如何在最新的 Microsoft.IdentityModel.Clients.ActiveDirectory 中使用 PromptBehavior 获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆