xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.53k stars 505 forks source link

WebAuthenticator in Android 11 #1626

Open jaysonragasa opened 3 years ago

jaysonragasa commented 3 years ago

I have a project that is running perfectly in Android 8 to 10 and it uses WebAuthenticator for oAuth authentication.

The app is targeting Android 11, running it in an actual Android 10 device is ok, but when ran into Android 11 in both emulator and in actual device. It throws an error

 E/AppCenterCrashes( 8871): Unhandled Exception from source=AndroidEnvironment
01-08 23:56:24.502 E/AppCenterCrashes( 8871): System.NullReferenceException: Object reference not set to an instance of an object.
01-08 23:56:24.502 E/AppCenterCrashes( 8871):   at Xamarin.Essentials.WebAuthenticatorIntermediateActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00018] in D:\a\1\s\Xamarin.Essentials\WebAuthenticator\WebAuthenticatorIntermediateActivity.android.cs:24 
01-08 23:56:24.502 E/AppCenterCrashes( 8871):   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <4658d344ef4b4a429f9058200bb1b31c>:0 
01-08 23:56:24.502 E/AppCenterCrashes( 8871):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)

Expected Behavior

Should be able to authenticate in oAuth

Actual Behavior

Crashes the app

Basic Information

Reproduction Link

I cannot provide a reproducable link but here's the code. The highlighted part is where the error occurs image

jaysonragasa commented 3 years ago

Good thing is, it works in version 1.5.3.2 for Android 11

jamesmontemagno commented 3 years ago

See: https://github.com/xamarin/Essentials/issues/1586#issuecomment-747065477

jaysonragasa commented 3 years ago

Thanks @jamesmontemagno that fixes the issue

https://github.com/xamarin/Essentials/issues/1586#issuecomment-747065477

jaysonragasa commented 3 years ago

image Hi @jamesmontemagno sorry I have to reopen the issue.

Tester came back to me and reported it still crashing. I checked appcenter.ms and still pointing the same issue. Do you have any idea what's going on?

It worked on Android 11 emulator, but when tested on an actual device, it crashes.

jamesmontemagno commented 3 years ago

Hmmmm @mattleibow is it possible that all of the stuff could be null for extras? https://github.com/xamarin/Essentials/blob/main/Xamarin.Essentials/WebAuthenticator/WebAuthenticatorIntermediateActivity.android.cs#L24

Is there some other flag we need to add on the Activity?

jaysonragasa commented 3 years ago

version 1.5.3.2 works for Android 11 in both Emulator and in actual device

hackzai commented 3 years ago

Same here, after applied the extra setting in AndroidManifest.xml (#1586). The same exception still reported.

Px7-941 commented 3 years ago

@jamesmontemagno @mattleibow I have created an example project. It can be reproduced on Android 10 as well as Android 11. https://github.com/Px7-941/IssueWebAuthenticator1626

The test opens a fake page with a redirection to the schema "essentialsauth: //". The first part of the oauth authorization code flow is simulated. The problem only occurs if all browsers with Custom Tabs support have been uninstalled or deactivated.

I tested it with Opera, Firefox and Microsoft Edge. https://play.google.com/store/apps/details?id=org.mozilla.firefox https://play.google.com/store/apps/details?id=com.opera.browser https://play.google.com/store/apps/details?id=com.microsoft.emmx

It works with Google Chrome because of custom tabs support. https://play.google.com/store/apps/details?id=com.android.chrome Brave works only on Android 10. https://play.google.com/store/apps/details?id=com.brave.browser

1c3f0x84 commented 3 years ago

I have the same problem on my device Nokia 7.1 on Android 10.0 - API 29.

Update: It works only on the first try, the second one fails!

AndreyBespamyatnov commented 3 years ago

We have the same issue after updating to the new TargetFramework version 11

System.NullReferenceException: Object reference not set to an instance of an object.
  at Xamarin.Essentials.WebAuthenticatorIntermediateActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00018] in D:\a\1\s\Xamarin.Essentials\WebAuthenticator\WebAuthenticatorIntermediateActivity.android.cs:24 
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <b24a608f3e284c3e8fda498590205f05>:0 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.32(intptr,intptr,intptr)
    fromUnobservedEvent: AndroidEnvironment_UnhandledExceptionRaiser
02-11 00:29:03.005 I/mono-stdout(11761): System.NullReferenceException: Object reference not set to an instance of an object.
02-11 00:29:03.005 I/mono-stdout(11761):   at Xamarin.Essentials.WebAuthenticatorIntermediateActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00018] in D:\a\1\s\Xamarin.Essentials\WebAuthenticator\WebAuthenticatorIntermediateActivity.android.cs:24 
02-11 00:29:03.006 I/mono-stdout(11761):   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <b24a608f3e284c3e8fda498590205f05>:0 
02-11 00:29:03.006 I/mono-stdout(11761):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.32(intptr,intptr,intptr)
02-11 00:29:03.006 I/mono-stdout(11761):     fromUnobservedEvent: AndroidEnvironment_UnhandledExceptionRaiser
02-11 00:29:03.024 D/Mono    (11761): DllImport searching in: '__Internal' ('(null)').
AndreyBespamyatnov commented 3 years ago

I found the fix. Well: If your project's Target Android version is set to Android 11 (R API 30) you must update your Android Manifest with queries that are used with the new package visibility requirements. https://developer.android.com/preview/privacy/package-visibility

Please add that to your AndroidManifest.xml file

<queries>
    <intent>
        <action android:name="android.support.customtabs.action.CustomTabsService" />
    </intent>
</queries>
Px7-941 commented 3 years ago

@AndreyBespamyatnov Does it also work if you uninstall or disable all browsers that support custom tabs? I have a crash when I use a normal browser (Google Chrome disabled) and then come back to the app.

I hope this PR #1687 solves the problem.

AndreyBespamyatnov commented 3 years ago

@Px7-941 It sounds like another issue, and it is not related to current one #1626 But, I tested your scenario, I have almost the same behavior, I do not have a crash (I'm catching all exception) but the authentication just does not work.

MNADEEMCH commented 3 years ago

@Px7-941 It sounds like another issue, and it is not related to current one #1626 But, I tested your scenario, I have almost the same behavior, I do not have a crash (I'm catching all exception) but the authentication just does not work.

What do you mean @AndreyBespamyatnov. you don't have any crash and still it's not working then what's the isssue you are facing? Can you please elaborate?

AndreyBespamyatnov commented 3 years ago

@Px7-941 It sounds like another issue, and it is not related to current one #1626 But, I tested your scenario, I have almost the same behavior, I do not have a crash (I'm catching all exception) but the authentication just does not work.

What do you mean @AndreyBespamyatnov. you don't have any crash and still it's not working then what's the isssue you are facing? Can you please elaborate?

Hi, sorry, but I think I fixed my issue, and now it works. Just add needed intent action

MNADEEMCH commented 3 years ago

thanks @jamesmontemagno you save my time bug fix #1586 but there is an issue with WebAuth on iOS 12(iPhone 6) there is not any support to return to application. I confirmed my callback uri scheme because everything is working on other all devices on android and iOS but not on iOS 12. Is there any solution please guide me.