Xamarin SDK
How to integrate with a Xamarin app
This guide provides instructions on integrating Authgear with a Xamarin app. Supported packages include:
- Xamarin.Essentials 1.7.2 or higher
- Xamarin.Forms 5.0.0.2401 or higher
Signup for an Authgear Portal account in https://portal.authgearapps.com/. Or you can use your self-deployed Authgear.
From the Project listing, create a new Project or select an existing Project. After that, we will need to create an application in the project.
Portal
authgear.yaml (self-deployed)
Step 1: Create an application in the Portal
- 1.Go to Applications on the left menu bar.
- 2.Click ⊕Add Application in the top tool bar.
- 3.Input the name of your application and select Native App as the application type. Click "Save".
- 4.You will see a list of guides that can help you for setting up, then click "Next".

Create an application
Step 2: Configure the application
- 1.In your IDE (e.g. Visual Studio), define a custom URI scheme that the users will be redirected back to your app after they have authenticated with Authgear, e.g.
com.myapp.example://host/path
.[^1] - 2.Head back to Authgear Portal, fill in the Redirect URI that you have defined in the previous steps.
- 3.Click "Save" in the top tool bar and keep the Client ID. You can also obtain it again from the Applications list later.

Edit an application
If you wish to validate JSON Web Token (JWT) in your own application server, turn on "Issue JWT as access token".[^2] If you wish to forward authentication requests to Authgear Resolver Endpoint, leave this unchecked. See comparisons in Backend Integration.

oauth:
clients:
- name: your_app_name
client_id: a_random_generated_string
redirect_uris:
- "com.myapp.example://host/path"
grant_types:
- authorization_code
- refresh_token
response_types:
- code
- none
- Open Visual Studio
- Create a new project
- Choose the Xamarin.Forms template
- Search for "Authgear.Xamarin" on nuget.org and add it to your base project. https://www.nuget.org/packages/Authgear.Xamarin/
- Authgear.Xamarin targets MonoAndroid 12.0 on Android, and Xamarin.iOS10 on iOS. Update the target framework of the Android and iOS projects to match Authgear.Xamarin's target frameworks.
- Update Android and iOS project's Xamarin.Essentials to 1.7.2.
To finish the integration, setup the app to handle the redirectURI specified in the application. This part requires platform specific integration.
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Authgear.Xamarin;
using Xamarin.Forms;
namespace MyApp.Droid
{
[Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionView },
Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable },
DataScheme = "com.myapp.example")]
public class WebAuthenticationCallbackActivity : Xamarin.Essentials.WebAuthenticatorCallbackActivity
{
}
}
If your Android app is targeting API level 30 or above (Android 11 or above), you need to add a
queries
section to AndroidManifest.xml
.<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Other elements such <application> -->
<queries>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
</manifest>
In your MainActivity.cs
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Xamarin.Forms;
using Authgear.Xamarin;
namespace MyApp.Droid
{
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
// ...
var authgear = new AuthgearSdk(this, new AuthgearOptions
{
ClientId = CLIENT_ID,
AuthgearEndpoint = ENDPOINT
});
DependencyService.RegisterSingleton<AuthgearSdk>(authgear);
LoadApplication(new App());
// ...
}
// other methods are omitted for brevity.
}
}
<?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>
<!-- other entries ... -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.myapp.example://host/path</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.myapp.example</string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
</dict>
</array>
</dict>
</plist>
In your AppDelegate.cs
using Xamarin.Essentials;
using Xamarin.Forms;
using Authgear.Xamarin;
namespace MyApp.iOS
{
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
var authgear = new AuthgearSdk(app, new AuthgearOptions
{
ClientId = CLIENT_ID,
AuthgearEndpoint = ENDPOINT
});
DependencyService.RegisterSingleton<AuthgearSdk>(authgear);
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
return Xamarin.Essentials.Platform.OpenUrl(app, url, options);
}
public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
if (Xamarin.Essentials.Platform.ContinueUserActivity(application, userActivity, completionHandler))
return true;
return base.ContinueUserActivity(application, userActivity, completionHandler);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="testauthgear.MainPage">
<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<Button Text="Configure" Clicked="Configure_Clicked" />
<Button Text="Authenticate" Clicked="Authenticate_Clicked"/>
</StackLayout>
</ContentPage>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Authgear.Xamarin;
namespace MyApp
{
public partial class MainPage : ContentPage
{
private AuthgearSdk authgear;
public MainPage()
{
InitializeComponent();
authgear = DependencyService.Get<AuthgearSdk>();
}
async void Configure_Clicked(object sender, EventArgs e)
{
// You must configure the instance before use.
// Typically you should do this once on app launch.
await authgear.ConfigureAsync();
}