Skip to content
Skills Library
Programming · Building & coding

maui-authentication

/maui-authentication

Add authentication to .NET MAUI apps. Covers WebAuthenticator for generic OAuth 2.0 / social login, and MSAL.NET for Microsoft Entra ID (Azure AD) with broker support (Microsoft Authenticator), token caching, Conditional Access, platform-specific setup (Android, iOS, Windows), DelegatingHandler for bearer token API calls, and Blazor Hybrid integration. USE FOR: "add authentication", "OAuth login", "MSAL.NET", "social login", "WebAuthenticator", "Entra ID MAUI", "Azure AD login", "Google login MAUI", "Apple login MAUI", "token caching", "bearer token", "sign in". DO NOT USE FOR: secure local st

Repository
maui-skills ↗
Licence
MIT
Last repository change
06.07.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
1
Deprecation

No signals detected. Repository changed on 06.07.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Add authentication to .NET MAUI apps. Covers WebAuthenticator for generic OAuth 2.0 / social login, and MSAL.NET for Microsoft Entra ID (Azure AD) with broker support (Microsoft Authenticator), token caching, Conditional Access, platform-specific setup (Android, iOS, Windows), DelegatingHandler for bearer token API calls, and Blazor Hybrid integration. USE FOR: "add authentication", "OAuth login", "MSAL.NET", "social login", "WebAuthenticator", "Entra ID MAUI", "Azure AD login", "Google login MAUI", "Apple login MAUI", "token caching", "bearer token", "sign in". DO NOT USE FOR: secure local st

Triggers from the description

add authenticationOAuth loginMSAL.NETsocial loginWebAuthenticatorEntra ID MAUIAzure AD loginGoogle login MAUI
Original author description

Add authentication to .NET MAUI apps. Covers WebAuthenticator for generic OAuth 2.0 / social login, and MSAL.NET for Microsoft Entra ID (Azure AD) with broker support (Microsoft Authenticator), token caching, Conditional Access, platform-specific setup (Android, iOS, Windows), DelegatingHandler for bearer token API calls, and Blazor Hybrid integration. USE FOR: "add authentication", "OAuth login", "MSAL.NET", "social login", "WebAuthenticator", "Entra ID MAUI", "Azure AD login", "Google login MAUI", "Apple login MAUI", "token caching", "bearer token", "sign in". DO NOT USE FOR: secure local st

How it works

  1. Security: Never Embed Secrets
  2. WebAuthenticator Gotchas
  3. WebAuthenticator Checklist
  4. Choosing Between WebAuthenticator and MSAL.NET
  5. MSAL.NET Gotchas
  6. MSAL.NET Checklist
Original SKILL.md headings · 121 lines, 716 words
SKILL.md preview
---
name: maui-authentication
description: >
  Add authentication to .NET MAUI apps. Covers WebAuthenticator for generic
  OAuth 2.0 / social login, and MSAL.NET for Microsoft Entra ID (Azure AD)
  with broker support (Microsoft Authenticator), token caching, Conditional
  Access, platform-specific setup (Android, iOS, Windows), DelegatingHandler
  for bearer token API calls, and Blazor Hybrid integration.
  USE FOR: "add authentication", "OAuth login", "MSAL.NET", "social login",
  "WebAuthenticator", "Entra ID MAUI", "Azure AD login", "Google login MAUI",
  "Apple login MAUI", "token caching", "bearer token", "sign in".
  DO NOT USE FOR: secure local storage of tokens (use maui-secure-storage),
  Aspire-specific auth setup (use maui-aspire), or server-side Entra ID provisioning
  (use entra-id-aspire-provisioning).
---

# .NET MAUI Authentication

## Security: Never Embed Secrets

> ❌ **Never embed client secrets, API keys, or signing keys in a mobile app binary.** They can be extracted trivially via decompilation.

The correct pattern:
1. App calls `WebAuthenticator` pointing to **your server** endpoint
2. Server initiates the OAuth flow with the identity provider (holds the client secret)
3. Provider redirects back to your server with an auth code
4. Server exchanges the code for tokens and returns them to the app via the callback URI

## WebAuthenticator Gotchas

### ⚠️ Windows WebAuthenticator is broken

Windows WebAuthenticator is currently broken. See [dotnet/maui#2702](https://github.com/dotnet/maui/issues/2702). Use MSAL or a WinUI-specific workaround for Windows auth flows.

### ⚠️ Apple Sign In returns name/email only once

Apple only returns the user's name and email on the **first** sign-in. Cache them immediately — subsequent sign-ins won't include them.

### ⚠️ PrefersEphemeralWebBrowserSession

Set to `true` on iOS 13+ to force a fresh login prompt. When `false` (default), the auth session shares cookies with Safari — the user may be auto-logged in, which can confuse logout/switch-account flows.

### ⚠️ Callback URI mismatches

The most common auth failure is a URI scheme mismatch. The `CallbackUrl` in code must exactly match:
- Android: `DataScheme` + `DataHost` in the `IntentFilter`
- iOS: `CFBundleURLSchemes` in `Info.plist`
- Windows: `Protocol Name` in `Package.appxmanifest`

## WebAuthenticator Checklist

- [ ] Callback URI scheme matches across all platform configs and `CallbackUrl`
- [ ] Android has `WebAuthenticatorCallbackActivity` with correct `IntentFilter`
- [ ] Android 11+ has `<queries>` for Custom Tabs in the manifest
- [ ] iOS/Mac Catalyst has `CFBundleURLTypes` in `Info.plist`
- [ ] Client secrets are on the server, not in the app
- [ ] Tokens stored with `SecureStorage`, cleared on logout
- [ ] `TaskCanceledException` handled gracefully in UI

---

## Choosing Between WebAuthenticator and MSAL.NET

| Criteria | WebAuthenticator | MSAL.NET |
|----------|-----------------|----------|
| Identity provider | Any OAuth 2.0 / OIDC | Microsoft Entra ID |
| Broker support (SSO) | ❌ No | ✅ Microsoft Authenticator, Company Portal |
| Conditional Access / MFA | ❌ Manual | ✅ Built-in |
| Token cache & refresh | ❌ Manual (SecureStorage) | ✅ Automatic |
| Complexity | Simple | More setup |
| Use when | Google, Apple, generic OIDC | Entra ID / Azure AD, Microsoft Graph |

---

## MSAL.NET Gotchas

### ⚠️ Android: OnActivityResult is required

Forgetting `AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs` in `MainActivity.OnActivityResult` causes auth to hang silently after the browser returns.

### ⚠️ iOS: Keychain sharing is required

Without `Entitlements.plist` containing keychain group `com.microsoft.adalcache`, token caching fails silently and users are prompted to sign in every time.

### ⚠️ Handle MsalUiRequiredException

When `AcquireTokenSilent` throws `MsalUiRequiredException`, the cached token is expired and interaction is needed. Always fall back to `AcquireTokenInteractive`.

```csharp
// ❌ Ignoring MsalUiRequiredException — user gets a crash
var result = await _pca.AcquireTokenSilent(scopes, account).ExecuteAsync(ct);

// ✅ Fall back to interactive when silent fails
try
{
    result = await _pca.AcquireTokenSilent(scopes, account).ExecuteAsync(ct);
}
catch (MsalUiRequiredException)
{
    result = await _pca.AcquireTokenInteractive(scopes).ExecuteAsync(ct);
}
```

### ⚠️ Handle user cancellation gracefully

```csharp
// ✅ Don't treat cancellation as an error
catch (MsalClientException ex) when (ex.ErrorCode == "authentication_canceled")
{
    return null;  // User cancelled — not an error
}
```

### ⚠️ Blazor Hybrid: Auth happens at the MAUI layer

In MAUI Blazor Hybrid apps, authentication must happen at the **MAUI layer** (MSAL.NET), not in the Blazor WebView. Don't use `AddMicrosoftIdentityWebApp` or server-side OIDC patterns. Instead:
1. MAUI handles sign-in via `IAuthService` (MSAL.NET)
2. A custom `MsalAuthenticationStateProvider` exposes auth state to Blazor
3. `HttpClient` with `DelegatingHandler` attaches bearer tokens automatically

## MSAL.NET Checklist

- [ ] `Microsoft.Identity.Client` NuGet package added
- [ ] App registration created in Entra ID with correct redirect URIs
- [ ] `AuthConfig` / `appsettings.json` has ClientId, TenantId, Scopes
- [ ] Android: `AndroidManifest.xml` has `<queries>` for broker and browsers
- [ ] Android: `MainActivity.OnActivityResult` calls `AuthenticationContinuationHelper`
- [ ] iOS: `Info.plist` has `CFBundleURLSchemes` with `msauth.{BundleId}`
- [ ] iOS: `Entitlements.plist` has keychain group `com.microsoft.adalcache`
- [ ] iOS: `AppDelegate.OpenUrl` calls `AuthenticationContinuationHelper`
- [ ] `IAuthService` registered as singleton in DI
- [ ] `DelegatingHandler` attached to `HttpClient` for API calls
- [ ] Login/logout UI wired up
- [ ] `MsalUiRequiredException` handled (triggers interactive sign-in)
- [ ] `MsalClientException` with `authentication_canceled` handled gracefully
Files in this package (2)
  • SKILL.md 6 KB
  • references/authentication-api.md 17 KB

Versions

The database has no other skills named maui-authentication.

Database snapshot: 13.11.2026 · package version ccd1453b813b (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100entra-app-registration
    Guides Microsoft Entra ID app registration, OAuth 2.0 authentication, and MSAL integration. USE FOR: create app registration, register Azure AD app, configure OAuth, set up authentication, add API permissions, generate service principal, MSAL example, console app auth, Entra ID…
  • 100maui-aspire
    Guide for .NET MAUI apps consuming .NET Aspire-hosted backend services. Covers AppHost configuration, service discovery for mobile clients, HttpClient DI setup, Entra ID authentication with MSAL.NET for calling protected APIs, development workflow (Aspire dashboard + MAUI…
  • 100entra-app-registration
    Guides Microsoft Entra ID app registration, OAuth 2.0 authentication, and MSAL integration. USE FOR: create app registration, register Azure AD app, configure OAuth, set up authentication, add API permissions, generate service principal, MSAL example, console app auth, Entra ID…
  • 100microsoft
    Emulated Microsoft Entra ID (Azure AD) OAuth 2.0 / OpenID Connect for local development and testing. Use when the user needs to test Microsoft sign-in locally, emulate Entra ID OIDC discovery, handle Microsoft token exchange, configure Azure AD OAuth clients, work with Microsoft…
  • 90maui
    .NET MAUI跨平台应用开发专家助手。当用户需要进行Android/iOS/macOS/Windows跨平台应用开发、MAUI XAML、Shell导航或移动端跨平台开发时调用。
  • 96dotnet-maui
    .NET MAUI component and application patterns Triggers on: **/*.xaml, **/*.cs

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.