microsoft-authentication-library-for-dotnet
microsoft-authentication-library-for-dotnet copied to clipboard
Handle null intent in SetAuthenticationContinuationEventArgs (MSAL 2.1.0)
Is your feature request related to a problem? Please describe. SetAuthenticationContinuationEventArgs is the method used in when implementing MSAL for Xamarin Andorid, which handles the return from an interactive sign-in. It takes three arguments, requestCode (int), resultCode (Result) and data (Intent).
I am working with Xamarin.Forms and experiencing an issue on Android where the user triggers interactive sign-in (PublicClientApplication.AcquireTokenAsync), taps the home button, then launches the app again from the apps menu (rather than resuming from the overview). This results in the Intent being null and thus SetAuthenticationContinuationEventArgs fails.
Consider the example provided in the wiki:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
Following the described steps, data
will be null and, if unhandled, the app will crash.
Describe the solution you'd like
It would be good to have MSAL throw an MsalException
(or variant) so that I can handle this scenario gracefully within my sign in logic flow.
Describe alternatives you've considered Right now I am checking for a null intent within MainActivity.OnActivityResult and simply not calling SetAuthenticationContinuationEventArgs.
Additional context Steps to reproduce the scenario:
- Launch app and start interactive signin (where the browser is shown)
- Tap the device home button
- Open the apps menu and open the app from the menu
Rather than not calling SetAuthenticationContinuationEventArgs
, another workaround is to create a dummy intent:
if (data == null)
{
data = new Intent("ReturnFromEmbeddedWebview");
}
This is better than not calling SetAuthenticationContinuationEventArgs (which is breaking my sign-in logic). The only problem I can see here is that the interaction doesn't continue (for example, a B2C password reset which requires the user to enter a verification code that they receive via email).
@nhsimarsden - When you say that the app crashes when the intent is null, do you mean that MSAL throws a null reference exception or some other exception that causes the app to crash? If that is the case, then we should treat this as a bug.
@bgavrilMS - Yes, you are correct. Below is the crash detail I pulled from App Center which led me to discover this issue:
System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.Identity.Client.AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00043] in <cab90e5acfec49d3967e0c2b916afc82>:0
at MyApp.Droid.MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00009] in <37116bf06c794465b4f43f38136adf39>:0
at Android.App.Activity.n_OnActivityResult_IILandroid_content_Intent_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 requestCode, System.Int32 native_resultCode, System.IntPtr native_data) [0x00014] in <f31a0642206a4c38a61a3a5fda304db6>:0
at (wrapper dynamic-method) System.Object.6(intptr,intptr,int,int,intptr)
@bgavrilMS - any update on this one? I too am seeing these errors in App Center and Play Console.
@jennyf19 and @trwalke, FYI
Any update on this one?
@WilliamWatterson86 Unfortunately no update. Will try to get a look at this asap.
Marking for 4.3. Validate if still relevant and explore options if it is.
@nhsimarsden @WilliamWatterson86 Apologies for letting this issue slip through. :/ Can either of you confirm that this is still happening with the latest version of MSAL 4.2.1? I have not been able to reproduce it. Thanks so much.
@jennyf19 I haven’t seen it either, so looking good
@WilliamWatterson86 thanks for the quick reply.
@henrik-me I'll let you decide how to proceed on this.
Closing issue if it can't be reproduced. Please reopen if new information arises.
@bgavrilMS @jennyf19
I am still getting this issue in app center.
I am using the latest version of the library 4.8.2
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data)
MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data)
Activity.n_OnActivityResult_IILandroid_content_Intent_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 requestCode, System.Int32 native_resultCode, System.IntPtr native_data)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.15(intptr,intptr,int,int,intptr)
This is very easy to reproduce, repo attached: App1.zip
Maybe add a try-catch-all in that method, it also throws other exceptions, like this one:
Java.Lang.RuntimeException: ClassNotFoundException when unmarshalling: com.google.android.apps.docs.cello.data.CelloEntrySpec
JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)
JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)
Intent.GetStringExtra (System.String name)
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data)
MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data)
We're seeing this issue for some users as well. Anyone know of a suitable work-around that doesn't break auth flow?
@bgavrilMS Any update on this by chance?
@nhsimarsden Hey Ian, any chance you found a work-around for this that didn't break auth flow? I'm surprised this has gone under the MSAL team's radar this long.
@nhsimarsden Hey Ian, any chance you found a work-around for this that didn't break auth flow? I'm surprised this has gone under the MSAL team's radar this long.
Try-catch the method call. When data
contains data from the auth activity, it is not null and therefore shouldn't crash
So a null check on data
should be better than a trycatch. The problem is even worse when you start having other handlers (for other application logics) in the OnActivityResult. I'll try the null check workaround and wait for the fix.
Raising to P1 as there are many people impacted by this.
Looks like the broker can sometimes trigger this as well. We need to handle the null better.
Also seeing this issue for quite a few users - we just pushed our app out to 2,500+ devices with a new MSAL auth flow and within minutes were getting these crash reports. Will handle the null for now, but it's not a very elegant solution for the long term!
Current thinking on the solution for this to stop processing if intent is null, which I think some of the folks on this thread are doing now by catching the null ref?
Alternatively, we can throw an MSAL exception, but I am not sure there is enough information to make it a meaningful exception :(
Resolved in 4.15
This bug is still present in 4.39, at least the crash I reported here.
Maybe add a try-catch-all in that method, it also throws other exceptions, like this one:
Java.Lang.RuntimeException: ClassNotFoundException when unmarshalling: com.google.android.apps.docs.cello.data.CelloEntrySpec JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) Intent.GetStringExtra (System.String name) AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data)
A similar crash is:
Android.OS.BadParcelableException: 'ClassNotFoundException when unmarshalling: com.google.android.apps.docs.common.drivecore.data.CelloEntrySpec'
It happens after sharing content to the Google Drive app and then returning back to my app.
Won't fix Xamarin issue. Please reopen if it happens on Maui.