c# - 未从 ADAL 库调用 AuthenticationContext 委托(delegate)

标签 c# azure windows-phone-8.1 windows-authentication azure-active-directory

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.AcquireTokenAndContinueIWebAuthenticationContinuable 相同的实例;当您尝试为其提供一个新实例(不起作用)时,我遇到了一些问题,在这个答案中,它被称为下面的 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

然后,在 IWebAuthenticationContinuableContinueWebAuthentication 的实现中,您需要调用 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/

相关文章:

c# - Numpy.NET 获取值

c# - DataGridViewAutoFilter 忽略文化并在 Winforms 上以中文显示过滤器

powershell - 使用 MSBuild 打包云服务项目时出现 CSPack RemotingException

configuration - 如何从Azure配置文件获取所有配置设置?

c# - asp.net webforms 在代码隐藏中处理 404 notfound 错误?

c# - WPF ListBox 项目命令绑定(bind)

c# - 微软Azure : How to create sub directory in a blob container

windows-phone - 如何防止我的 Windows Phone 8.1 应用程序被盗版?

c# - 从 MediaCapture 访问预览帧

c# - Windows Phone 8.1 如何以编程方式创建具有指定模板的磁贴