Some of the scenarios require to open the external/system browser to perform oAuth 2.0 login and then receive the auth code via redirect URL scheme. To open the system browser in .NET MAUI we can use the Launcher functionality. Here’s a complete explanation and sample code for Single Sign-On (SSO) using Launcher.
Before starting with our implementation we have to set-up some prerequisites:
- Registered app in Azure AD
- Redirect URI like webauthenticator://callback
- OAuth 2.0 with PKCE
Set Up Platform-Specific URI Scheme
Android: WebAuthenticatorCallbackActivity .cs
iOS: Info.plist
Create the Authorization Request
Create the auth request using the Launcher functionality to perform SSO login
MainPage.xaml.cs
Helper methods like PkceUtil.CreateCodeVerifier() and PkceUtil.CreateCodeChallenge() are available in my Git repository LauncheroAuth/PkceUtil.cs
Handle Redirect in OnAppLinkRequestReceived
App.xaml.cs
To respond to an Android and iOS lifecycle delegate being invoked, call the ConfigureLifecycleEvents method on the MauiAppBuilder object in the CreateMauiapp method of your MauiProgram class.
Sample code is available in this Git repo — https://github.com/GovardhanNag/oAuthUsingLauncher.git