我正在使用Windows Phone 8.1应用程序,并希望通过Azure Active Directory身份验证对访问后端Web Api的用户进行身份验证。我已经参考了Git Hub示例(https://github.com/Azure-Samples/active-directory-dotnet-windowsphone-8.1),当在按钮的onclick事件上编写以下代码时,示例对我来说很好用
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(todoListResourceId, clientId);
if (result != null && result.Status == AuthenticationStatus.Success)
{
// A token was successfully retrieved. Post the new To Do item
AddTodo(result);
}
else
{
authContext.AcquireTokenAndContinue(todoListResourceId, clientId, redirectURI, AddTodo);
}
但是,当我在异步函数中编写它并在主页面中调用它时,它会抛出以下异常。
{Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException: authentication_ui_failed: The browser based authentication dialog failed to complete ---> System.Exception: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
at Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAndContinue(Uri requestUri, Uri callbackUri, ValueSet continuationData, WebAuthenticationOptions options)
at Microsoft.IdentityModel.Clients.ActiveDirectory.WebUI.Authenticate(Uri authorizationUri, Uri redirectUri, IDictionary`2 headersMap, CallState callState)
--- End of inner exception stack trace ---
at Microsoft.IdentityModel.Clients.ActiveDirectory.WebUI.Authenticate(Uri authorizationUri, Uri redirectUri, IDictionary`2 headersMap, CallState callState)
at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenInteractiveHandler.AcquireAuthorization()
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireTokenAndContinueCommon(String resource, String clientId, Uri redirectUri, UserIdentifier userId, String extraQueryParameters, AuthenticationContextDelegate authDelegate)
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireTokenAndContinue(String resource, String clientId, Uri redirectUri, AuthenticationContextDelegate authDelegate)
at TodoListClient.MainPage.<MainPageAfterLoad>d__0.MoveNext()
ErrorCode: authentication_ui_failed}``
我也尝试将此代码添加到main_Loaded函数中,但没有帮助。