AcquireTokenAsync 在 UWP 应用中失败

AcquireTokenAsync fails in a UWP app

我在我的 Win10 UWP 应用程序中使用 ADAL。这是一段代码:

// 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;

有时它会因以下错误而失败,而不会显示身份验证 window:

authentication_ui_failed: The browser based authentication dialog failed to complete. Value does not fall within the expected range.

偶尔,它确实会显示身份验证 window 但无法重定向,生成:

"authentication_ui_failed: The browser based authentication dialog failed to complete. The system cannot locate the resource specified. (Exception from HRESULT: 0x800C0005)"

这使用了库的 WinRT 版本。使用 .NET 版本的类似代码在控制台应用程序中运行良好:

// .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 商店应用程序来说并不常见。