1

An example of the implementation for the ADAL library for Windows Phone 8.1 can be found on GitHub

To display the Azure Login page for the user you call the AuthenticationContext.AcquireTokenAndContinue method. After the login process it should do a callback to the 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
}

The AuthContextDelegateMethod is not getting called even after a successful login.

Is there any reason why the AuthenticationContextDelegate is not called, and any way to fix that?

Barnstokkr
  • 2,904
  • 1
  • 19
  • 34
Rahul Sonone
  • 2,685
  • 1
  • 27
  • 38

1 Answers1

1

I had this same issue with the ADAL library not working and not calling the AuthContextDelegateMethod

The firts thing was to understand how the AndContinue methods work, once you understand this the rest make a lot of sense.

App.xaml.cs

In your App.xaml.cs you need to add the OnActivate method which works with the ContinuationManager; this is where the app enters after the Azure login returns.

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);        
    }
}

The GetIWebAuthenticationContinuableInstance should return the same instance of the IWebAuthenticationContinuable that called AuthenticationContext.AcquireTokenAndContinue; I had some issues when you try to give it a new instance (didn't work), in this answer it is called AzureLoginClass below.

ContinuationManager

Then in the ContinuationManager I added the following function (which is an adaptation of 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;
    }
}

I needed to do this because I implemented my IWebAuthenticationContinuable as part of a class and the given implementations give preference to an AppicationPage.

AzureLoginClass

Then in your implementation of IWebAuthenticationContinuable's ContinueWebAuthentication you need to call the 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);
   }
}

When ContinueAcquireTokenAsync is called it gives a callback to AuthContextDelegateMethod. This is however not called if result.ErrorDescription is "User canceled authentication", so if you need this you should cater extra for it.

Community
  • 1
  • 1
Barnstokkr
  • 2,904
  • 1
  • 19
  • 34