UWP app中的AcquireTokenAsync失败 [英] AcquireTokenAsync fails in a UWP app
问题描述
我在Win10 UWP应用中使用ADAL。这是一段代码:
I'm using ADAL in my Win10 UWP app. Here is a snippet of code:
// WinRT, UWP app. Doesn't work
TokenCache TC = new TokenCache();
authContext = new AuthenticationContext(authority, true, TC);
var authresult = await authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri));
token = authresult.AccessToken;
有时它会失败,并显示以下错误,而不会出现身份验证窗口:
Sometimes it fails with the following error, without ever bringing up the auth window:
authentication_ui_failed:基于浏览器的身份验证对话框无法完成。值不在预期范围内。
有时,它确实会打开身份验证窗口,但无法重定向,从而产生:
Occasionally, it does bring up the auth window but fails to redirect, producing:
authentication_ui_failed:基于浏览器的身份验证对话框无法完成。系统无法找到指定的资源。(来自HRESULT的异常:0x800C0005)
这使用库的WinRT版本。使用.NET版本的类似代码可从控制台应用程序很好地工作:
This uses a WinRT version of the library. A similar code using .NET version works great from the console app:
// .NET, console app. Works great
TokenCache TC = new TokenCache();
authContext = new AuthenticationContext(authority, TC);
var authresult = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri));
token = authresult.AccessToken;
推荐答案
这通常是由商店和UWP的沙箱引起的应用。应用程序的重定向uri至少应与运行时分配的重定向uri相匹配-请参阅github.com/azuread上的Windows存储示例。其他可能影响行为的因素包括包装盒上的隐私设置,使用本地网络而不要求提供正确的功能..:适用于Windows应用商店的所有限制也将适用于ADAL。
另外:我能问你为什么要将自定义缓存传递给应用程序吗?对于在诸如Windows商店应用之类的沙盒环境中运行的应用来说,这种情况并不常见。
This is typically caused by the sandboxing of store and UWP apps. At a minimum, the redirect uri of the app should match the one assigned by the runtime - see the windows store sample on github.com/azuread. Other things that might impact the behavior are privacy settings on the box, use of local network without asking for the correct capabilities..: all the restrictions that apply to windows store apps will apply to the use of ADAL as well. Also: can I ask you why you are passing a custom cache to the app? That's not usual for apps running on sandboxes environments like the windows store apps.
这篇关于UWP app中的AcquireTokenAsync失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!