[Bug] WebAuthenticator TaskCanceledException thrown after Successful Authentication
Description
On Android, when Xamarin.Essentials.WebAuthenticator.AuthenticateAsync returns from a successful authentication, it always throws System.Threading.Tasks.TaskCanceledException:
Note: This behavior does not happen on iOS
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.Threading.Tasks.TaskCanceledException: A task was canceled.
[mono-rt] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.1(intptr,intptr)
[mono-rt] at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.1(intptr,intptr)
Reproduciton Steps
- Download/Clone this repo, https://github.com/brminnick/WebAuthenticatorRepro
- In Visual Studio, open
WebAuthenticatorRepro.sln - In Visual Studio, set the startup project to WebAuthenticator.Android
- In Visual Studio, build/deploy WebAuthenticator.Android to an Android Device/Emulator
- On the Android device, click
Connect to GitHub - On the Android device, connect using your GitHub username/password
- Confirm WebAuthenticator.Android crashes with
System.Threading.Tasks.TaskCanceledException
Expected Behavior
AuthenticateAsync should return the valid authentication results
Actual Behavior
AuthenticateAsync throws System.Threading.Tasks.TaskCanceledException
Basic Information
- Version with issue: 1.5.3.2
- Last known good version: Unknown
- Android: Xamarin.Android v10.3.99.259
Screenshots

Reproduction Link
https://github.com/brminnick/WebAuthenticatorRepro
Solution
Looks like the problem was caused by placing the [IntentFilter] on the MainActivity instead of putting it on the WebAuthenticatorCallbackActivity.
Orignal Code (Throws TaskCanceledException)
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
namespace WebAuthenticatorRepro.Droid
{
[Activity(Label = "WebAuthenticatorRepro", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { Android.Content.Intent.ActionView }, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, DataScheme = OAuthConstants.Scheme)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
protected override void OnResume()
{
base.OnResume();
Xamarin.Essentials.Platform.OnResume();
}
}
}
Working Code
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
namespace WebAuthenticatorRepro.Droid
{
[Activity(Label = "WebAuthenticatorRepro", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
protected override void OnResume()
{
base.OnResume();
Xamarin.Essentials.Platform.OnResume();
}
}
[Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter(new[] { Android.Content.Intent.ActionView }, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, DataScheme = OAuthConstants.Scheme)]
public class WebAuthenticatorActivity : Xamarin.Essentials.WebAuthenticatorCallbackActivity
{
}
}

User error
Hi @brminnick ,
I am also encountering this error. Followed your solution but error still persisting. in my setup in the subclass my scheme I used is e.g. "xamarinessentials" and in the authenticationasync callback uri I am using "xamarinessentials://ABC". Do you think this causes the error? When I try to use same value "xamarinessentials://ABC in the subclass and uri I am getting error "need to subclass with same uri". Any thoughts?