mistelltein
mistelltein

Reputation: 121

Android MAUI App: GeneralSecurityException during Release Authentication but Works in Debug

I am developing a mobile application using .NET MAUI. Everything works perfectly in debug mode, but when I switch to the release mode and try to authenticate, I encounter the following error:

Exception_WasThrown Java.Security.GeneralSecurityException

Here are the details of my setup and what I've tried so far:

.NET MAUI application targeting net8.0-android. Visual Studio 2022. Using SecureStorage to store tokens. Authentication with a backend service.

Verified permissions in AndroidManifest.xml. Cleaned and rebuilt the project.

The error only occurs when the authentication is successful.

I am seeking assistance to understand why this issue occurs only in the release build and how to resolve it. Any insights or suggestions would be greatly appreciated.

Thank you!

[RelayCommand]
    private async Task Login()
    {
        try
        {
            var tokenResponse = await _authenticationService.AuthenticateAsync(PinGet!, PasswordGet!);
            if (tokenResponse == null)
            {
                await Shell.Current.DisplayAlert("Error", "Authentication failed", "OK");
                return;
            }

            await SecureStorage.SetAsync("access_token", tokenResponse.JwtToken!);
            await SecureStorage.SetAsync("refresh_token", tokenResponse.RefreshToken!);

            await Shell.Current.DisplayAlert("Success", "Logged in successfully", "OK");
            await Shell.Current.GoToAsync(nameof(HomePage));
        }
        catch (Exception ex)
        {
            await Shell.Current.DisplayAlert("Error", $"An error occurred: {ex.Message}", "OK");
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Mobile.Mobile">
    <application android:allowBackup="true" android:icon="@mipmap/mainicon2" android:supportsRtl="true" android:usesCleartextTraffic="true" android:label="Mobile">
    </application>
    
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BATTERY_STATS" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-feature android:name="android.hardware.location" android:required="false" />
    <uses-feature android:name="android.hardware.location.gps" android:required="false" />
    <uses-feature android:name="android.hardware.location.network" android:required="false" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.USE_BIOMETRIC" />
    <uses-permission android:name="android.permission.USE_FINGERPRINT" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34" />
</manifest>

Upvotes: 4

Views: 632

Answers (1)

mistelltein
mistelltein

Reputation: 121

The issue was likely with SecureStorage. This problem appeared recently, although the code hasn't changed significantly.

Following the suggestions, I added these lines to the csproj:

<PropertyGroup Condition="$(Configuration)=='Release'">
    <RunAOTCompilation>false</RunAOTCompilation>
    <PublishTrimmed>false</PublishTrimmed>
</PropertyGroup>

Afterwards, during authentication, I encountered the: "javax.crypto.AEADBadTagException" error, which I resolved by disabling backups in the manifest:

<application android:allowBackup="false" ... >

These solutions helped fix the issues in the release application.

Upvotes: 4

Related Questions