question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebAuthenticator crash with some browsers

See original GitHub issue

Description

Recently I released a new app with .NET MAUI (you can see here) and it behaves very different from device to device. With some browsers (for example Mi browser) app crashes when user is redirected back to the app with WebAuthenticator.

Here is the exception:

Exception android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
   at Microsoft.Maui.Authentication.WebAuthenticatorIntermediateActivity.OnCreate(Bundle )
   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr , IntPtr , IntPtr )
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V , IntPtr , IntPtr , IntPtr )
  at crc6468b6408a11370c2f.WebAuthenticatorIntermediateActivity.n_onCreate
  at crc6468b6408a11370c2f.WebAuthenticatorIntermediateActivity.onCreate (WebAuthenticatorIntermediateActivity.java:33)
  at android.app.Activity.performCreate (Activity.java:8142)
  at android.app.Activity.performCreate (Activity.java:8114)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3549)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3748)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2187)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:236)
  at android.app.ActivityThread.main (ActivityThread.java:8057)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:620)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1011)

I was trying to turn off shrinks, aot and everything else, nothing helps.

Installed Workload Id      Manifest Version       Installation Source
--------------------------------------------------------------------------------
maui-android               7.0.59/7.0.100         SDK 7.0.100, VS 17.4.33213.308
maui-ios                   7.0.59/7.0.100         SDK 7.0.100, VS 17.4.33213.308
maui-maccatalyst           7.0.59/7.0.100         SDK 7.0.100, VS 17.4.33213.308
maui-tizen                 7.0.59/7.0.100         SDK 7.0.100
maui-windows               7.0.59/7.0.100         VS 17.4.33213.308
maccatalyst                16.2.1024/7.0.100      VS 17.4.33213.308
ios                        16.2.1024/7.0.100      VS 17.4.33213.308
android                    33.0.26/7.0.100        VS 17.4.33213.308

Steps to Reproduce

Select Mi browser as your default browser and try to sign in with Google

Link to public reproduction project repository

I can’t share my project

Version with bug

7.0 (current)

Last version that worked well

Affected platforms

Android

Affected platform versions

Android 11 (SDK 30), Android 10 (SDK 29)

Did you find any workaround?

No

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
FM1973commented, May 31, 2023

@mattleibow Are you going to backport this to .net7?

3reactions
ShadowOfPhantomcommented, Mar 8, 2023

@ShadowOfPhantom Do you find workaround? I had this issue too!

Nah, but I did something custom. I had to release fixed version of app asap so my implementation is not elegant but it works. Well, I made this:

[Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)]
[IntentFilter(new[] { Intent.ActionView },
              Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable, Intent.ActionView },
              DataScheme = CALLBACK_SCHEME)]
public class AuthActivity : Activity
{
    const string CALLBACK_SCHEME = "xochatbot";

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        if (this.Intent != null && !string.IsNullOrWhiteSpace(this.Intent.DataString))
            Preferences.Set("signinstring", this.Intent.DataString);
        
        StartActivity(MainActivity.MainAct.Intent);
        Finish();
    }
}

Then you can pull “signinstring” from preferences on your main activity. You probably can make it better if you know how activity works, but I prefer simplicity here: MAUI can throw exceptions where you don’t even expect them.

Thanks @ShadowOfPhantom for sharing the workaround. I have one 2 questions:

  • On the StartActivity line, you are sending as parameter the Intent, however, the MainAct or name of main activity is not being returned for me. am I missing something? -> I think I fixed this by using:
Intent i = new Intent(Android.App.Application.Context, typeof(MainActivity));
StartActivity(i);
  • Could you share how you are pulling the signingstring on your main activity, please?

Thanks again for sharing the solution to solve this issue.

1) MainAct is my referrence of MainActivity as a static field:

 public class MainActivity : MauiAppCompatActivity
    {
        public static Activity MainAct;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (MainAct is null)
                MainAct = this;
        }
...}

With this reference you can return to your actual activity with ease after sign in flow 😃

2) On resume activity I invoke an event to check on my sign in page if sign in string has appeared on preferences:

protected override void OnResume()
        {
            base.OnResume();

            App.NeedExtraHandleLoginState?.Invoke();
        }

If string exists I parse it and continue sign in flow.

I’d repeat that this is working solution but I made it urgently to fix app crashes after intial release of my app. So if you have a bit more time to realize how intents and activities work then you probably can make and share with us a better workaround. For now I prefer msft team to fix WebAuthenticator to get it work as its best because I have not enough understanding how it works on Android, and where I can go wrong there. I already see too much crashes (I don’t want to make things even worse) on my Google Play Console and have no idea why, where and how I can fix them, seems like it’s some MAUI issues that going to be fixed in a future with upcoming releases of MAUI ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebAuthenticationBroker.AuthenticateAsync freeze in ...
Most of them occur in asynchronous method processing and UI rendering. If your app first freezes, and then crashes after some time, then...
Read more >
Xamarin Android WebAuthenticator bug
Im having a problem using WebAuthenticator on Xamarin. ... I tryed removing the activity from the manifest, and now the browser wont open....
Read more >
Add Authentication to .NET MAUI Apps with Auth0
After it returns browser result, browser gets opened with error and post closing the browser popup, app displays blank in black like crashed....
Read more >
WebAuthn Passwordless Login
Only some uncommon browsers still lack support for WebAuthn. Authentication[edit]. To login you must enter your username in the login form ...
Read more >
Sign users out
Sign users out of Okta by clearing the Okta browser session. ... An optional step is to revoke the tokens so they can't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found