azure – 如何在最新的Microsoft.IdentityModel.Clients.ActiveD
在旧版本的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(); 当我运行这个时,我得到错误 不确定这是否是由于我在控制台应用程序上运行.如果是这样,我如何让它工作? 解决方法
您收到此错误的原因是因为您为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的另一个重载来使用仅限app的流程: /// <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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |