Architecture note: Xamarin.Essentials SecureStorage for Xamarin.Forms app secrets
Use Xamarin.Essentials SecureStorage for small per-app secrets in Xamarin.Forms, with exception handling and a migration path to .NET MAUI.
27 Aug 2026, 04:59 UTC

Problem and useful takeaway
You need to persist a user’s OAuth refresh token or an app-level encryption key on a mobile device. Storing it in ordinary application preferences writes it to plain-text files, and Xamarin.Forms shared code has no built-in encrypted store. Xamarin.Essentials SecureStorage exposes platform-native encrypted key-value storage—iOS Keychain on iOS and EncryptedSharedPreferences backed by Android KeyStore—from a single asynchronous API.
Takeaway: use SecureStorage only for small, per-app secrets, wrap it in your own abstraction, handle FeatureNotSupportedException, and treat Xamarin.Essentials as a maintenance-mode bridge to .NET MAUI rather than a long-term platform.
Requirements
SecureStorage is a fit when:
- the secret is a short string, such as a token, key identifier, or opaque value;
- per-app isolation is acceptable;
- deletion on app uninstall is acceptable;
- no other app, app extension, or separate process needs to read the value;
- asynchronous read/write is acceptable through a service layer.
It is not a fit for large binary blobs, general application settings, values that must roam across devices, or data requiring automatic rotation or a schema.
Smallest suitable design
Do not call SecureStorage directly from pages or view models. Define a small abstraction in shared code:
public interface IAppSecretStore
{
Task<string> GetAsync(string key);
Task SetAsync(string key, string value);
Task RemoveAsync(string key);
}
Implement it with SecureStorage. Because SecureStorage stores strings only, serialize structured data yourself and apply additional encryption only if your threat model requires defense beyond the platform’s encryption at rest.
public class SecureAppSecretStore : IAppSecretStore
{
public async Task<string> GetAsync(string key)
{
try
{
return await SecureStorage.GetAsync(key);
}
catch (FeatureNotSupportedException)
{
return null;
}
}
public async Task SetAsync(string key, string value)
{
try
{
await SecureStorage.SetAsync(key, value);
}
catch (FeatureNotSupportedException ex)
{
throw new InvalidOperationException("Secure storage is unavailable on this device.", ex);
}
}
public async Task RemoveAsync(string key)
{
try
{
await SecureStorage.RemoveAsync(key);
}
catch (FeatureNotSupportedException)
{
// No secure storage to clean up.
}
}
}
Initialize Xamarin.Essentials in each platform project before calling SecureStorage. In Android MainActivity call Xamarin.Essentials.Platform.Init(this, savedInstanceState); in iOS AppDelegate call Xamarin.Essentials.Platform.Init(). Missing initialization typically produces runtime failures rather than build errors.
Adding the package
Run from a terminal in the solution or project directory:
dotnet add <project-name> package Xamarin.Essentials
Required permissions: none beyond normal build permissions. Expected check: the project file contains a PackageReference for Xamarin.Essentials after the command completes. Risk: version conflicts with other Xamarin packages can block resolution; verify the project still builds before writing code against the API.
Trust and data boundaries
- Scope is per-app and bound to the application package ID. Other installed apps cannot read the values.
- Values are typically removed on uninstall. Backup behavior varies by platform and is not guaranteed; do not design around restore behavior you have not verified.
- App extensions and app groups are not supported by this API; do not use SecureStorage for cross-process secrets.
- On rooted or jailbroken devices, and on managed devices with security policies, native Keychain or KeyStore guarantees may be weakened or disabled.
Operational checks
- On a physical iOS device and a physical Android device, set a secret, restart the app, and read it back. Persistence across restarts confirms the native store is wired correctly.
- Run on a device or emulator with no lock screen or an unsupported OS version and confirm
FeatureNotSupportedExceptionis thrown where expected. - Confirm the value does not appear as plain text in the app’s shared preferences or user defaults using platform-specific tooling; this verifies encryption at rest rather than a plain-text fallback.
- Wrap every call in try/catch and log failures without writing the secret value to logs.
Failure modes
FeatureNotSupportedExceptionon unsupported OS versions or when secure storage is unavailable.- Implicit size limits; do not store large blobs.
- No built-in schema or rotation. If the underlying device key material changes, your app must re-encrypt and re-store affected values.
- Serialization exceptions if you attempt to store non-string types without a wrapper.
Rollback
SetAsync changes state. Rollback for a single key means calling RemoveAsync(key) or overwriting the key with the previous value. There is no transaction spanning multiple keys, so multi-secret updates require your own compensation logic.
Conditions that would change the design
- You need secrets shared with an app extension or another process: move to platform-specific access groups instead of SecureStorage.
- You need structured records, automatic rotation, or auditability: add an envelope-encryption layer and key management outside SecureStorage.
- You are starting new development: prefer .NET MAUI, because Xamarin.Essentials and the Xamarin SDKs are in maintenance and end-of-support.
Verification before release
Test on physical devices, not only emulators, because emulator secure-storage behavior can differ from production hardware. Confirm exception handling for FeatureNotSupportedException, confirm values survive a restart, and confirm the package still builds after adding Xamarin.Essentials. Treat platform backup and uninstall behavior as unverified until you observe it on your target OS versions.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.