Using SecureStorage with MAUI

Cloud storage

I’m creating a new project called LanguageInUse with NET8 and MAUI and I like to save the user credentials using the SecureStorage. Like in other cases, there are a few things that you can find but you must be done in order to have a working project.

Let me show you how to configure your MAUI project properly. No settings are required for Windows.

Apple

So, looking at the documentation, I couldn’t find any explanation for why my application was crashing when it tried to save or delete something from the SecureStorage. The reason is that for iOS, I have to declare that I want to use secure storage.

Add an Entitlements.plist file

For this reason, I have to instruct the iOS to allow the application to use the SecureStorage. I have to add a new file.

To add a new entitlements file to your .NET MAUI app project, add a new XML file named Entitlements.plist to the Platforms\iOS folder of your app project. Then add the following XML to the file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>

Entitlements can be configured in Visual Studio by double-clicking the Entitlements.plist file to open it in the entitlements editor.

  1. In Solution Explorer, double-click the Entitlements.plist file from the Platforms > iOS folder of your .NET MAUI app project to open it in the entitlements editor.
  2. In the entitlements editor, select and configure any entitlements required by your app
Entitlements in Visual Studio - Using SecureStorage with MAUI
Entitlements in Visual Studio

Save the changes to your Entitlements.plist file to add the entitlement key/value pairs to the file.

Consume entitlements

A .NET MAUI iOS app must be configured to consume the entitlements defined in the Entitlements.plist file.

  1. In Solution Explorer, right-click on your .NET MAUI app project and select Properties. Then, navigate to the iOS > Bundle Signing tab.
  2. In the Bundle Signing settings, click the Browse… button for the Custom Entitlements field.
  3. In the Custom Entitlements dialog, navigate to the folder containing your Entitlements.plist file, select the file, and click the Open button.
  4. In the project properties, the Custom Entitlements field will be populated with your entitlements file:
iOS Bundle Signing in Visual Studio - Using SecureStorage with MAUI
iOS Bundle Signing in Visual Studio

Android

The steps I have taken to resolve this error are:

  1. Creating a proguard.cfg file in “Project/Platforms/Android/” in your file system.
  2. In your .csproj file, set it as a ProguardConfiguration:
  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
    <ProguardConfiguration Include="Platforms\Android\proguard.cfg" />
  </ItemGroup>
Properties of the  - Using SecureStorage with MAUI
  1. In your proguard.cfg file, add the following lines (you may be able to use less broad rules like in your original post but I have not tested this):
-keep class androidx.security.crypto.** { *; }
-keep class com.google.crypto.tink.** { *; }
  1. Configure selective backup for MAUI essentials: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/secure-storage?view=net-maui-8.0&tabs=android#selective-backup

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.