最新如何使用PromptBehavior获取Token Microsoft.IdentityModel.Clients.ActiveDirectory
How to AcquireToken with PromptBehavior in the latest Microsoft.IdentityModel.Clients.ActiveDirectory
在 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();
当我运行这个我得到错误
{"Invalid owner window type. Expected types are IWin32Window or IntPtr (for window handle)."}
不确定这是否是因为我 运行正在使用控制台应用程序。如果是这样,我该如何让它工作?
您收到此错误的原因是您在 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,它在编译时被接受,因为它是一个对象,但在 运行 时不被接受,因为它不是 window。
要解决此问题,只需不传入此参数或将其作为 null 传入即可。这将使您的控制台应用程序启动 window,提示用户登录。
如果这是一个控制台应用程序,应该 运行 没有 任何用户交互,那么你应该通过这个使用仅限应用程序的流程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 参数的 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();
当我运行这个我得到错误 {"Invalid owner window type. Expected types are IWin32Window or IntPtr (for window handle)."}
不确定这是否是因为我 运行正在使用控制台应用程序。如果是这样,我该如何让它工作?
您收到此错误的原因是您在 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,它在编译时被接受,因为它是一个对象,但在 运行 时不被接受,因为它不是 window。
要解决此问题,只需不传入此参数或将其作为 null 传入即可。这将使您的控制台应用程序启动 window,提示用户登录。
如果这是一个控制台应用程序,应该 运行 没有 任何用户交互,那么你应该通过这个使用仅限应用程序的流程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)