This tutorial explains how to integrate the Auth0 OIDC Application with a Xamarin application.

Install the Auth0.OidcClient.Android NuGet Package

If you are using Visual Studio 2017, simply open the Package Manager Console (View -> Other Windows -> Package Manager Console), and install the package:

For Android:

Install-Package Auth0.OidcClient.Android

Did it help?/

For iOS:

Install-Package Auth0.OidcClient.iOS

Did it help?/

Alternatively, if you are using Visual Studio for Mac, please perform the following steps:

With the project loaded in Visual Studio for Mac, Ctrl+click (or right click) on the Packages folder of the project in the Solution Pad, and select Add Packages...

The Add Packages dialog will appear. Search and locate the package called Auth0.OidcClient.Android or Auth0.OidcClient.iOS depending on your platform.

Tick the checkbox next to the package to select it, and click the Add Package button

Set Up the Auth0 Callback URL

Callback URLs are the URLs that Auth0 invokes after the authentication process. Auth0 routes your application back to this URL and appends additional parameters to it, including an access code which will be exchanged for an id_token, access_token and refresh_token.

Since callback URLs can be manipulated, you will need to add your application's URL to your application's Allowed Callback URLs for security. This will enable Auth0 to recognize these URLs as valid. If omitted, authentication will not be successful.

Then, call the LoginAsync method which will redirect the user to the login screen. You will typically do this in the event handler for a UI control such as a Login button.

var loginResult = await client.LoginAsync();

Did it help?/

Handing the callback URL

After a user has logged in, they will be redirected back to your application at the Callback URL that was registered before. In both Android and iOS you need to handle this callback to complete the authentication flow.

Android

Register an intent which will handle this callback URL. An easy way to do this is to register the intent on the same activity from which you called the LoginAsync method to initiate the authentication flow.

Replace YOUR_ANDROID_PACKAGE_NAME in the code sample above with the actual Package Name for your application, such as com.mycompany.myapplication. Also ensure that all the text for the DataScheme, DataHost, and DataPathPrefix is in lower case. Also, set LaunchMode = LaunchMode.SingleTask for the Activity, otherwise the system will create a new instance of the activity every time the Callback URL gets called.

Now write code to handle the intent. You can do this by overriding the OnNewIntent method. Inside the method you need to call the Send method on the ActivityMediator to complete the authentication cycle:

You need to handle the Callback URL in the OpenUrl event in your AppDelegate class. You need to notify the Auth0 OIDC Client to finish the authentication flow by calling the Send method of the ActivityMediator singleton, pass along the URL that was sent in: