maui
maui copied to clipboard
Preventing NullReferenceException on WebAuthenticatorIntermediateActi…
Description of Change
Preventing random NullReferenceException on WebAuthenticatorIntermediateActivity.android.cs
Hey there @ederbond! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.
Hello, does this also apply to NetworkOnMainThreadException, wich appears randomly on Authentication for Rest APIs? Greetings
Hi @LAXStudios, I'm not sure. I just got a NullReferenceException that was happening randomly when I try do authenticate with Identity Server.
/azp run
Azure Pipelines successfully started running 2 pipeline(s).
Any news about this @jfversluis and @mattleibow ?
@ederbond did you actually verify this works? I can imagine that maybe this line might now break because this value is going to be null?
https://github.com/dotnet/maui/pull/12347/files#diff-9dd7f7fa057499c8357247f9f40fe5abc4890c315fc8451563c48b840c9af101R46
Still, this wouldn't be worse than what is happening now. Just wondering if we have a full solution here.
Hi @jfversluis I didn't tested it cause I don't know how to compile a modifield version of .NET MAUI locally and use it on my own Maui Project. But this is a random error that happen time to time when I try to authenticate using OAuth. The stacktrace that Visual Studio 2022 shows me, make it cristal clear that there was a NullReference exception on that line.
BTW I couldn't navigate the line you asked me a question on your previous reply: This link doesn't righlight the line you're refering to on your previous reply: https://github.com/dotnet/maui/pull/12347/files#diff-9dd7f7fa057499c8357247f9f40fe5abc4890c315fc8451563c48b840c9af101R46
I don't know how to compile a modifield version of .NET MAUI locally and use it on my own Maui Project.
This might be helpful https://github.com/dotnet/maui/blob/main/.github/DEVELOPMENT.md#compile-using-a-local-bindotnet, see:
dotnet cake --sln="<download_directory>\MauiApp2\MauiApp2.sln" --target=VS
It does not affect your system if you follow the "Compile using a local bin\dotnet" section. If you are on Windows, then https://github.com/dotnet/maui/issues/12762#issuecomment-1406656020 is important too.
Azure Pipelines successfully started running 2 pipeline(s).
Thank you for your pull request. We are auto-formating your source code to follow our code guidelines.
/azp run
Azure Pipelines successfully started running 2 pipeline(s).
Hi, @mattleibow is it going to be merged? Time to time I bump into an app crash because of this. But since it's a random error it's hard to reproduce, I'm quite confident this PR will fix the issue.
We're currently not planning on backporting this because we don't have a repro and we haven't tested this fix. We feel there is most likely still an issue inside the OnResume
call that will occur. Once we can test this fix we will reconsider backporting.
While that my MAUI app will still be randomly crashing most of the time when I ask it to logoff.
I'm using
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
to authenticate against my Identity Server.
Sometimes when I call IdentityModel.OidcClient.LogoutAsync() it crashes with a NullReferenceException on that line that I have changed on this PR. But since it doesn't happen very oftem (random) it's hard to create a repro test.
Unfortunately, this pull request does not solve the whole problem. I was able to debug the behavior a bit closer. When I set the Opera browser as default, no custom tabs are used. The default browser is started.
https://github.com/dotnet/maui/blob/main/src/Essentials/src/WebAuthenticator/WebAuthenticator.android.cs#L78
This probably causes that the activity is not started and so the intent extras are null.
https://github.com/dotnet/maui/blob/main/src/Essentials/src/WebAuthenticator/WebAuthenticatorIntermediateActivity.android.cs
The null check is not sufficient as suspected.
if(extras == null) { return; }
https://github.com/dotnet/maui/pull/12347/commits/104b87b5bda520ca315f437fef7a2d0640f0193c
I was able to fix it with a null check.
https://github.com/dotnet/maui/blob/main/src/Essentials/src/WebAuthenticator/WebAuthenticatorIntermediateActivity.android.cs#L46
StartActivity(actualIntent ?? Intent);
Maybe someone could test this with the Mi Browser.
I would like to test the workaround @Px7-941 shared. However, I added the Activity class: WebAuthenticatorIntermediateActivity.cs in the /Platforms/Android folder using same Maui namespace to see if get overwritten without luck.
do you have any idea how to overwrite it in any .NEt MAUI app, so I can test using the Mi Browser?
Thanks in advance
Does anyone have a sample test case to illustrate the original problem or the problem that @PureWeen alludes to potentially existing in issue #13978 ? And is it the same issue that @Px7-941 is hitting?