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.