Xamarin Android: Create Login With Web API Using Azure SQL Server - Part One

First we create Azure SQL Server Database. Go to Azure Portal.

Create Azure SQL server database

Step 1

Create SQL Database and Database server. Go to portal bottom + DATA SERVICES->SQL DATABASE->QUICK CREATE then give your database name, server name, and password.  After that click create SQL Database.

Step 2

Next open SQL Database Design View,  then Create New Table and name for Login.

 

Step 3

Then Create Login Table Rows. Here create three rows:  ID, Username, Password. After creating rows go to Visual studio and create New WEB API.
 

WEB API

Step 4

Open Visual Studio->New Project->Templates->Visual C#->Web ->ASP.NET Web Application, then open new pop up window and select WEB API. Then give Project Name and Project Location.

 
 
 

Step 5

Next go to Solution Explorer-> Project Name-> Models then Right Click to Add -> ADO.NET Entity Data Model then open new Dialog box then Give Item Name and follow the given steps.
 
  

Step 6

Select EF Designer from Database.

 

Step 7

Select New Connection. 

  

Step 8

Here give Server name, Username and password then finally select database.

 
Step 9

  

Step 10

  

Step 11

Next go to Solution Explorer-> Project Name-> App Start -> WebApiConfig.cs. open cs code then give below code at the end of register method. This code will be converted from XML format to JSON format in output.

  1. var json = config.Formatters.JsonFormatter;  
  2.   
  3. json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;  
  4.   
  5. json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();  
  6.   
  7. config.Formatters.Remove(config.Formatters.XmlFormatter);  

Step 12

Next go to Solution Explorer-> Project Name-> Controllers then Right Click to Add->Controllers then Open New Popup window you can choose Web API 2 Controller with read/write actions after give controller name.
 
 
 
Step 13

Then Open once you have created ControllerName.cs. Here you create two methods, Xamarin_reg and Xamarin_login.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using XamarinLogin.Models;  
  8. namespace XamarinLogin.Controllers  
  9. {  
  10.     public class LoginController: ApiController  
  11.     {  
  12.         xamarinloginEntities db = new xamarinloginEntities();  
  13.         /  
  14.         [HttpPost]  
  15.         [ActionName("XAMARIN_REG")]  
  16.         // POST: api/Login  
  17.         public HttpResponseMessage Xamarin_reg(string username, string password)  
  18.         {  
  19.             Login login = new Login();  
  20.             login.Username = username;  
  21.             login.Password = password;  
  22.             db.Logins.Add(login);  
  23.             db.SaveChanges();  
  24.             return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created");  
  25.         }  
  26.         [HttpGet]  
  27.         [ActionName("XAMARIN_Login")]  
  28.         // GET: api/Login/5  
  29.         public HttpResponseMessage Xamarin_login(string username, string password)  
  30.         {  
  31.             var user = db.Logins.Where(x => x.Username == username && x.Password == password).FirstOrDefault();  
  32.             if (user == null)  
  33.             {  
  34.                 return Request.CreateResponse(HttpStatusCode.Unauthorized, "Please Enter valid UserName and Password");  
  35.             }  
  36.             else  
  37.             {  
  38.                 return Request.CreateResponse(HttpStatusCode.Accepted, "Success");  
  39.             }  
  40.         }  
  41.     }  
  42. }  


Next steps --  How to Publish WEB API  in  Azure Portal.

Step 14

Next go to Solution Explorer-> Project Name then Right Click to Click Publish then open new popup window for Publish Web.

  

Step 15
  
Step 16
  
Step 17
  
Step 18
  
Step 19
 
 
Finally, we have successfully created and Hosted  WEB API Using Azure SQL Server database.