This tutorial has been tested with the following:
- Microsoft Visual Studio 2015
- Xamarin for Visual Studio 4.2
This tutorial explains how to integrate Auth0 with a Xamarin application.
Xamarin.Auth0Client helps you authenticate users with any Auth0 supported identity provider via the OpenId Connect protocol built on top of OAuth2. The library is cross-platform, so this information can be applied to either iOS or Android.
NOTE: An Objective-C Binding Library for Lock iOS implementations is available at: Lock.Xamarin.
Install Xamarin.Auth0Client Component
In order to include
Xamarin.Auth0Client component, please perform the following steps:
- With the project loaded in Xamarin Studio (or Visual Studio), right-click on the
Componentsfolder in the
Solution Explorerand select
Get More Components.
- Search and double-click on
- From the component page, select the
Add to Projectbutton to download the component and add it to the current project.
For more information, see: How to include a Component in a Xamarin Project.
Set up the Auth0 Callback URL
Go to the Application Settings section in the Auth0 dashboard and make sure that Allowed Callback URLs contains the following value:
There are three options for implementing the integration:
- Use the Auth0 Lock inside a Web View - the simplest option with only a few lines of code required.
- Create your own UI - more work, but higher control over the UI.
- Use a specific username and password.
Option 1: Auth0 Lock
Lock is the recommended option.
Here is a snippet of code to paste into your project:
using Auth0.SDK; var auth0 = new Auth0Client( "YOUR_NAMESPACE", "YOUR_CLIENT_ID");
// 'this' could be a Context object (Android) or UIViewController, UIView, UIBarButtonItem (iOS) var user = await auth0.LoginAsync(this); /* - get user email => user.Profile["email"].ToString() - get facebook/google/twitter/etc access token => user.Profile["identities"]["access_token"] - get Windows Azure AD groups => user.Profile["groups"] - etc. */
Xamarin.Auth0Client is built on top of the
WebRedirectAuthenticator in the Xamarin.Auth component. All rules for standard authenticators apply regarding how the UI will be displayed.
Option 2: Custom User Interface
If you know which identity provider you want to use, you can add the
connection parameter and the user will be directed to the specified
var user = await auth0.LoginAsync(this, "google-oauth2"); // connection name here
NOTE: Connection names can be found on Auth0 dashboard (e.g.
Option 3: Specific Username and Password
var user = await auth0.LoginAsync( "sql-azure-database", // connection name here "email@example.com", // user name "1234"); // password
Access User Information
Auth0User has the following properties:
Profile: returns a
Newtonsoft.Json.Linq.JObjectobject from Json.NET component containing all available user attributes (e.g.:
IdToken: a JSON Web Token (JWT) containing all of the user attributes and signed with your client secret.
access_tokenthat can be used to call the Auth0 APIs. For example, you could use this token to Link Accounts.
Android and iOS samples are available on GitHub at: Xamarin.Auth0Client.