c# – Canceling consent flow in MAUI WebAuthenticator crashes the app


The .NET Maui Webauthenticator is working well for our server-based authentication flow on iOS and Android platforms. So this might be a rather odd question because I can get the authorization just fine. The problem is when the user cancels out of the Consent or hits the back button. Doing so is lethal in both platforms.

ivs consent page

The linked document says:

If the user cancels the flow at any point, a TaskCanceledException is thrown.

So we allow for this:

try
{
    authResult = await WebAuthenticator.Default.AuthenticateAsync(
    new WebAuthenticatorOptions()
    {
        Url = new Uri(ivs_auth_broker),
        CallbackUrl = new Uri(Constants.Android.RedirectUri),
        PrefersEphemeralWebBrowserSession = true
    });
    code = authResult?.Properties["code"];
    if (string.IsNullOrEmpty(code))
    {
        Debug.Fail("OAuth code not received.");
        return false;
    }
    else
    {
        return await exchangeCodeForTokens(
        GoogleAuthConsts.TokenUrl,
        authResult!.Properties["code"],
        Constants.Android.ClientId,
        Constants.Android.RedirectUri);
    }
}
catch (TaskCanceledException) 
{ 
    return false;
}
catch
{
    return false;
}

The exceptions are being caught. In the debugger, by breaking on the line and stepping through the crash can even be averted. But when break points are removed, or if the app is built in Release mode, it just ugly dies.

We’re also keeping a close watch on the back button action in hopes of cancelling, but it’s not being invoked by one of “our” pages, it’s happening in the WebView which promptly crashes everything.

protected override bool OnBackButtonPressed()
{
    return base.OnBackButtonPressed();
}

Looking for a graceful way to handle this condition please.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img