Essentials icon indicating copy to clipboard operation
Essentials copied to clipboard

[Bug] WebAuthenticator TaskCanceledException thrown after Successful Authentication

Open TheCodeTraveler opened this issue 6 years ago • 3 comments

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

  1. Download/Clone this repo, https://github.com/brminnick/WebAuthenticatorRepro
  2. In Visual Studio, open WebAuthenticatorRepro.sln
  3. In Visual Studio, set the startup project to WebAuthenticator.Android
  4. In Visual Studio, build/deploy WebAuthenticator.Android to an Android Device/Emulator
  5. On the Android device, click Connect to GitHub
  6. On the Android device, connect using your GitHub username/password
  7. 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

TheCodeTraveler avatar Jun 04 '20 23:06 TheCodeTraveler

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
    {

    }
}

TheCodeTraveler avatar Jun 04 '20 23:06 TheCodeTraveler

User error

davehump avatar Jun 08 '20 15:06 davehump

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?

denz96 avatar Apr 17 '21 01:04 denz96