ADAL library for Windows Phone 8.1 的实现示例可以在 GitHub 上找到
要为调用AuthenticationContext.AcquireTokenAndContinue
的用户显示Azure登录页面方法。登录过程结束后,它应该回调 AuthenticationContextDelegate
.
AuthenticationContext _authenticationContext = AuthenticationContext.CreateAsync(authority).GetResults();
_authenticationContext.AcquireTokenAndContinue(resource, clientId, redirectUri, authenticationContextDelegate);
AuthenticationContextDelegate authenticationContextDelegate= new AuthenticationContextDelegate(AuthContextDelegateMethod);
public static void AuthContextDelegateMethod(AuthenticationResult result)
{
// Never called
}
AuthContextDelegateMethod
即使成功登录后也不会被调用。
有什么原因导致 AuthenticationContextDelegate
没有被调用,有什么方法可以解决这个问题吗?
最佳答案
我也遇到了同样的问题,ADAL 库无法工作且未调用 AuthContextDelegateMethod
首先要了解 AndContinue
如何方法是有效的,一旦你理解了这一点,剩下的就很有意义了。
应用程序.xaml.cs
在您的App.xaml.cs中,您需要添加与ContinuationManager
配合使用的OnActivate
方法;这是 Azure 登录返回后应用程序进入的位置。
ContinuationManager _continuationManager;
protected override void OnActivated(IActivatedEventArgs e)
{
base.OnActivated(e);
_continuationManager = new ContinuationManager();
var continuationEventArgs = e as IContinuationActivatedEventArgs;
if (continuationEventArgs != null)
{
IWebAuthenticationContinuable azure = GetIWebAuthenticationContinuableInstance();
_continuationManager.Continue(continuationEventArgs, azure);
}
}
GetIWebAuthenticationContinuableInstance
应返回与调用 AuthenticationContext.AcquireTokenAndContinue
的 IWebAuthenticationContinuable
相同的实例;当您尝试为其提供一个新实例(不起作用)时,我遇到了一些问题,在这个答案中,它被称为下面的 AzureLoginClass。
延续管理器
然后在 ContinuationManager我添加了以下函数(这是 Continue(IContinuationActivatedEventArgs args, Frame rootFrame)
的改编版):
internal void Continue(IContinuationActivatedEventArgs args, IWebAuthenticationContinuable wabPage)
{
if (args == null)
throw new ArgumentNullException("args");
if (this.args != null && !handled)
throw new InvalidOperationException("Can't set args more than once");
this.args = args;
this.handled = false;
this.id = Guid.NewGuid();
if (wabPage == null)
return;
switch (args.Kind)
{
case ActivationKind.PickFileContinuation:
break;
case ActivationKind.PickSaveFileContinuation:
break;
case ActivationKind.PickFolderContinuation:
break;
case ActivationKind.WebAuthenticationBrokerContinuation:
if (wabPage != null)
{
wabPage.ContinueWebAuthentication(args as WebAuthenticationBrokerContinuationEventArgs);
}
break;
}
}
我需要这样做,因为我将 IWebAuthenticationContinuable
实现为类的一部分,并且给定的实现优先考虑 AppicationPage。
AzureLoginClass
然后,在 IWebAuthenticationContinuable
的 ContinueWebAuthentication
的实现中,您需要调用 ContinueAcquireTokenAsync
public class AzureLoginClass : IWebAuthenticationContinuable
{
public void CallAzureLogin
{
_authenticationContext.AcquireTokenAndContinue(resource, clientId, redirectUri, AuthContextDelegateMethod);
}
private void AuthContextDelegateMethod(AuthenticationResult result)
{
// Is now called when ContinueAcquireTokenAsync is called
}
public async void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
{
AuthenticationResult result = await _authenticationContext .ContinueAcquireTokenAsync(args);
}
}
当调用 ContinueAcquireTokenAsync
时,它会回调 AuthContextDelegateMethod
。然而,如果 result.ErrorDescription
是 “用户取消了身份验证”
,则不会调用此函数,因此,如果您需要此函数,则应为其提供额外服务。
关于c# - 未从 ADAL 库调用 AuthenticationContext 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27483922/