Integrating Auth Service into a Xamarin Android App
Tags: huawei, android, xamarin, authentication
Published: 2022-01-10 13:52:44

Xamarin (Microsoft) is a multi-system development platform for mobile services that many developers use. Many AppGallery Connect services now support Xamarin, including Auth Service. Here I'll explain on how to integrate Auth Service into your Xamarin.Android app that requires mobile number sign-in support.

## Install the Xamarin environment.

You'll need to first download and install Visual Studio 2019.
Open Visual Studio and select Mobile development with .NET to install the Xamarin environment.
 
![Visual Studios install screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9oknn2l1tkpohgllnje.png)

Next make sure you have enabled the [Auth Service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-getstarted-0000001053053922) in AppGallery Connect.

Open Visual Studio, click Create a new project in the start window, select Mobile App (Xamarin.Forms), and set the app name and other required information.

![Visual Studio create new project screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qoffdg2w5i54hpru8bmy.png)

Right-click your project and choose **Manage NuGet Packages**.
![Right click menu with Manage NuGet Packages highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tx9wcmxwx5s1jzca0vc.png)

Search for the **Huawei.Agconnect.Auth** package on the displayed page and install it.
![NuGet search result for Huawei.Agconnect.Auth](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr5ft4ny5pr3o2pd9jqr.png)
 
Download the JSON service file from your AppGallery project and add it into the **Assets* directory in your project.
![Xamarin file structure showing agconnect-service.json file in Asserts folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpbvccmjn60i3u9usrpy.png)
 
Create a new class named **HmsLazyInputStreams.cs**, and implement the following code to read the JSON file.
{% gist https://gist.github.com/devwithzachary/8ad7abd19fb983df197dc2833430baee %}

Then add the following code to **AttachBaseContext** under **MainActivity**.
{% gist https://gist.github.com/devwithzachary/e69f6052294df98bb8c9d1f101f117b8 %}

Right-click your project and choose Properties. Click Android Manifest on the displayed page, and set a package name.
![Android Manifest settings screen, with package name option highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6bq8101b1wmgtcjm36p.png)
 
Once you've completed all of these preparations, you'll be able to develop app functions.

If your app requires mobile number sign-in support, the Auth Service SDK can help you implement both sign-up and sign-in for this authentication mode. You'll need to send verification codes to your users for both stages. Auth Service can help you with that as well.

## Setup Mobile Number verification

Create a VerifyCodeSettings object that contains the SMS messaging settings, including the action and language.
{% gist https://gist.github.com/devwithzachary/7b32e47f42fd9a9b17966fc1e56c4630  %}

Call the **RequestVerifyCodeAsync** method to send a request to the Auth Service server, and pass the country code and mobile number entered by a user, and the **VerifyCodeSettings** object you just created, for Auth Service to send a verification code SMS message to the user.
{% gist https://gist.github.com/devwithzachary/fe45edba090e47b9e54a337522ba30bb %}

Upon receiving the verification code, the user can start sign-up.
First, you'll need to create a PhoneUser object to store the user's inputs, including the mobile number, country code, verification code, and password. The user can choose whether to set a password. If so, they'll need to enter a password when signing in to your app.
{% gist https://gist.github.com/devwithzachary/636da7132edf559a5a283395a21403a6 %}

Call the **CreateUserAsync** method to create a user.
{% gist https://gist.github.com/devwithzachary/5698e3981cd3a6d13fd242d5dabab216 %}

Once sign-up is complete, the Auth Service SDK will automatically sign the user in to your app, and you won't need to call the sign-in API again.

For an existing user, you need to implement the sign-in process, either via a verification code or a password.
{% gist https://gist.github.com/devwithzachary/f7a58adf5185ef664f335dc956ab09b0 %}

You can call CredentialwithPassword or CredentialWithVerifyCode to generate a credential for a password sign-in or a verification code sign-in, respectively. Then call the SignInAsync method to pass the credential for sign-in.

And thats it! We now have a fully functional authentication system using the users mobile number to confirm they are who they say they are.