Login And SignUp In Xamarin.Forms

In this article, I will explain a complete method of creating a login and signup page using Web API and MVC.

Introduction

Login

Login is a process of logging in to an application, database, or device etc. Most of the login pages require an email and password to be entered by the users.

SignUp
 
SignUp is a process of signing in to an application. Another term used for SignUp is registration. A user can provide his/her credentials to register himself/herself into the application. A signup form requires a username, email, password, DOB, gender and other such details for registration.
 

Procedure

  1. First of all, create a new Web project. In that project, add a class in Models. For this, go to the Solution Explorer, right click on the model -> add -> class. Give a suitable name to that class say “User”. In user.cs, add the following by writing property and double tap on it. Set the datatype and property for that.
    1. public class User  
    2.      {  
    3.         public int Id { getset; }  
    4.         public string Email { getset; }  
    5.         public string Password { getset; }  
    6.       }  
    7. }  
  1. Now, build the solution before adding a Controller to that model class. After building add a Controller. For this, go to the Solution Explorer, right click on controller->add->controller. Add MVC 5 with views using entity frameworks using same name as in model class click on add. A controller is added into your solution. Now run your solution. Type http://localhost:61525/Users/Create on browser after creating you will be able to edit, create new, delete etc.

  2. For webApi add a controller again by following same procedure and select WebApi 2 controller with actions using entity frameworks. Using same name as used for previous controller. A new controller will be added named “User1conroller”. Run your solution. Type http://localhost:61525/api/Users1 this on your browser. You will see,

    Output
Login and SignUp in Xamarin.Forms 

Procedure for combining Login services to this WebAPI

  1. Go to your computer’s task bar. Right click on Visual Studio and open a new project. Select cross platform. Right click on your project in solution explorer and add a class with same name “User”. Add same properties to that class.
    1. public class User  
    2.         {  
    3.         public int Id { getset; }  
    4.         public string Email { getset; }  
    5.         public string Password { getset; }  
    6.      }  
    7. }  
  1. In MainPage.Xaml Design your Login page’s front end. Add the following piece of code:
    1. <StackLayout HorizontalOptions="Center" VerticalOptions="Center">  
    2.         <Entry Placeholder="Email" x:Name="Email"/>  
    3.         <Entry Placeholder="Password" x:Name="Pass"/>  
    4.         <Button Text="Login" x:Name="Login" Clicked="Login_Clicked"></Button>  
    5. </StackLayout>  
  1. Go to solution explorer add a folder of name ServiceHandler and add a class to that folder of name LoginService. Add following code,
    1. RestClient<User> _restClient = new RestClient<User>();  
    2. Public async Task<bool> CheckLoginIfExists(string email, string password)  
    3.    {  
    4.       var check = await _restClient.checkLogin(email, password);  
    5.       return check;  
    Certain type of errors occur. If you take your pointer to Task<bool> a flash bulb appears click on it and add a threading method.
  1. Now add a folder by following same method with name RestApiClass and add a class in it named RestClient. Add following piece of code,
    1. classRestClient < T > {  
    2.     Private  
    3.     const string MainWebServiceUrl = "https://jammovies.azurewebsites.net/";  
    4.     Private  
    5.     const string LoginWebServiceUrl = "https://jammovies.azurewebsites.net/api/UserCredentials/";  
    6.     Public async Task < bool > checkLogin(string email, string password) {  
    7.         var httpClient = new HttpClient();  
    8.         var response = await httpClient.GetAsync(LoginWebServiceUrl + "email=" + email + "/" + "password=" + password);  
    9.         return response.IsSuccessStatusCode;  
    10.     }  
    11. }  
    Give permission to HttpClient going to flash bulb and by clicking first option. You have to type <T> after classRestClient.
  1. Now go to MainPage.xaml.cs add the following piece of code in the event created in Login Button.
    1. Private async void Login_OnClicked(object sender, EventArgs e) {  
    2.     LoginService service = new LoginService();  
    3.     var getLoginDetails = await service.CheckLoginIfExists(Email.Text, Pass.Text);  
    4.     if (getLoginDetails) {  
    5.         await DisplayAlert("Login Successfull""Username or Password is correct""Okay""Cancel");  
    6.     } else if (Email.Text == null && Pass.Text == null) {  
    7.         await DisplayAlert("Login failed""Enter your Email and Password before login""Okay""Cancel");  
    8.     } else {  
    9.         await DisplayAlert("Login failed""Username or Password is incorrect or not exists""Okay""Cancel");  
    10.     }  
    11. }  
  1. Open WebApi project and in User1conroller add the following piece of code,
    1. [HttpGet]  
    2. [Route("api/UserCredentials/email={email}/password={password}")]  
    3. Public async Task < IHttpActionResult > UserDetailsLogin(string email, string password) {  
    4.     User login = await db.Users.Where(x => x.Email == email && x.Password == password).SingleOrDefaultAsync();  
    5.     if (login == null) {  
    6.         return NotFound();  
    7.     }  
    8.     return Ok(login);  
    9. }  
    Run WebApi and copy the link from browser and pase it in http:// links in RestClient class,
    1. classRestClient < T > {  
    2.     Private const string MainWebServiceUrl = http: //localhost:61525/;    
    3.     Private const string LoginWebServiceUrl = http: //localhost:61525/api/UserCredentials/;    
    4.         Public async Task < bool > checkLogin(string email, string password) {  
    5.             var httpClient = new HttpClient();  
    6.             var response = await httpClient.GetAsync(LoginWebServiceUrl + "email=" + email + "/" + "password=" + password);  
    7.             return response.IsSuccessStatusCode;  
    8.         }  
    9. }  

Run WebApi first. Then run xamarin solution. If you type correct email and password I will show a display alert like this,

Login and SignUp in Xamarin.Forms 

And if you type incorrect Email ad Password it will show the following display alert,

Login and SignUp in Xamarin.Forms 

Procedure of Combining SignUp services to WebApi

  1. First of all, create a new Web project. In that project add a class in Model. For this go to solution explorer right click on model -> add -> class. Give a suitable name to that class say “User”. In user.cs add he following by writing prop and double tab on it. Set data type and property for that.
    1. public class User  
    2. {  
    3.     public int Id { getset; }  
    4.     public string FirstName { getset; }  
    5.     public string LastName { getset; }  
    6.     public string Email { getset; }  
    7.     public string Password { getset; }  
    8.     public string ConfirmPassword { getset; }  
    9. }  
  1. Build your solution and add MVC AND WebApi controllers by following same procedure as described above. Run your solution. Register yourself by creating. After adding MVC and after adding WebApi controller you can see your signup form

  2. Now open a new project with cross platform. First of all add a class with same name “User” and give same properties o ha class as given in web project.

  3. Go to MainPage.xaml and add signup frontend as,
    1. <StackLayout HorizontalOptions="Center" VerticalOptions="Center">  
    2.         <Entry Placeholder="First Name" x:Name="First"/>  
    3.         <Entry Placeholder="Last Name" x:Name="Last"/>  
    4.         <Entry Placeholder="Email" x:Name="Email"/>  
    5.         <Entry Placeholder="Password" x:Name="Pass"/>  
    6.         <Entry Placeholder="Confirm Password" x:Name="Confirm"/>  
    7.         <Button Text="Register" CornerRadius="3" x:Name="Register" Clicked="Register_Clicked"></Button>  
    8. </StackLayout>  
  1. Methods for adding ServiceHandler and RestApi are same.

  2. Go to MainPage.xaml.cs and add he following piece of code in button event,
    1. LoginService service = new LoginService();  
    2. var getLoginDetails = await service.CheckLoginIfExists(Email.Text, Pass.Text);  
    3.   
    4. if (First.Text==null && Last.Text==null && Email.Text == null && Pass.Text == null && Confirm.Text==null )  
    5.             {  
    6.                 await DisplayAlert("Registration Failed""Enter your details""Okay""Cancel");  
    7.             }  
    8.             else if (getLoginDetails)  
    9.             {  
    10.                 await DisplayAlert("Registration Failed"" Already have an account""Okay""Cancel");  
    11.             }  
    12.             else  
    13.             {  
    14.                 await DisplayAlert("Registration successful""Thanks for using our app""Okay""Cancel");  
    15.             }  
    16.         }  
    17.     }  
    18. }  
  1. Open WebApi project and in User1conroller add the following piece of code,
    1. [HttpGet]  
    2. [Route("api/UserCredentials/email={email}/password={password}")]  
    3. Public async Task < IHttpActionResult > UserDetailsLogin(string email, string password) {  
    4.     User login = await db.Users.Where(x => x.Email == email && x.Password == password).SingleOrDefaultAsync();  
    5.     if (login == null) {  
    6.         return NotFound();  
    7.     }  
    8.     return Ok(login);  
    9. }  
  1. Run WebApi and copy the link from browser and paste it in http:// links in RestClient class,
    1. classRestClient < T > {  
    2.     Private  
    3.     const string MainWebServiceUrl = “http: //localhost:50226/”;  
    4.         Private  
    5.     const string LoginWebServiceUrl = “http: //localhost:50226/UserCredentials/”;  
    6.         Public async Task < bool > checkLogin(string email, string password) {  
    7.             var httpClient = new HttpClient();  
    8.             var response = await httpClient.GetAsync(LoginWebServiceUrl + "email=" + email "/" + "password=" + password);  
    9.             return response.IsSuccessStatusCode;  
    10.         }  
    11. }  

This is the whole procedure. Thank You.