User Verification Service Using Web API

Introduction

In this article, we will learn, how to send the verification code, using Web API and here, I am using Twilio Service.

Twilio provide Services to send the verification code with call and text. In this article, I am using Twilio calling Service. For this, we need to install two NuGet packages, as shown in the figure, given below-


Afterwards, visit Twilio's official site and register itself( https://www.twilio.com). After registration, you need to provide your account Sid, Auth token, Phone number, as shown in the depicted figure, given below-


Here, we use Web API to develop the verification Service .

Create MVC Application

Step 1 - Go to File-> New-> Project.

Step 2 - Choose "ASP.NET MVC 4 Web Application" from the list, provide the Application name as " TwilioService" and set the path in the location input, where you want to create the Application.

Step 3 - Now, choose the Project Template "Web API".

Step 4 - Add API empty controller and class with name “Twilio”

Step 5 - Create model class with name Verification and it contains two properties. 

  1. public class Verification  
  2.   
  3. {  
  4.   
  5. public string MobileNumber { getset; }  
  6.   
  7. public string VerificationCode { getset; }  
  8.   
  9. }   

Afterwards, create method inside Twilio controller with the name VerificationCall. 

  1. public class TwilioController : ApiController  
  2.     {  
  3.         [HttpPost]  
  4.         [ActionName("VerificationCall")]  
  5.         public HttpResponseMessage VerificationCall(Verification verification)  
  6.         {  
  7.             HttpResponseMessage response = new HttpResponseMessage();  
  8.             //get twilio accountsid and authentication key  
  9.             string accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"].ToString();  
  10.             string authToken = ConfigurationManager.AppSettings["TwilioAuthKey"].ToString();  
  11.             //get number which provided by twilio  
  12.             string twilioNumber = ConfigurationManager.AppSettings["TwilioNumber"].ToString();  
  13.             try  
  14.             {  
  15.                 var client = new Twilio.TwilioRestClient(accountSid, authToken);  
  16.                 if (string.IsNullOrEmpty(verification.MobileNumber))  
  17.                 {  
  18.                     response = Request.CreateResponse(HttpStatusCode.NotFound, "wrong number");  
  19.                 }  
  20.                 else  
  21.                 {  
  22.                     Regex regex = new Regex(@"^[0-9]*$");  
  23.                     string mobileno = string.Empty;  
  24.                     if (verification.MobileNumber.Contains("-"))  
  25.                     {  
  26.                         string[] number = verification.MobileNumber.Split('-');  
  27.                         mobileno = number[1];  
  28.                     }  
  29.                     Match m = regex.Match(verification.MobileNumber);  
  30.                     //get autoriztion code which need to be for user authorization  
  31.                     verification.VerificationCode = string.Empty;  
  32.                     Random random = new Random();  
  33.                     string pinCode = (random.Next() % 90000 + 10000).ToString();  
  34.                     //it for check which process user want to authorization call/sms.  
  35.                     verification.VerificationCode = pinCode.ToString();  
  36.   
  37.                         //voice message  
  38.                         string message = "Welcome%20to%20Atul%20Your%20authorization%20code%20is%20" + HttpUtility.UrlPathEncode(String.Join<char>(" ", pinCode.ToString())) + "&";  
  39.                         //voice call url  
  40.                         string content = "http://twimlets.com/message?Message%5B0%5D=" + message + "";  
  41.   
  42.                         //call option which is used to provide caller phone no and twilio url for call  
  43.                         var options = new Twilio.CallOptions();  
  44.                         options.Url = content;  
  45.                         options.From = twilioNumber;  
  46.                         options.To = ConfigurationManager.AppSettings["CountryCode"] + verification.MobileNumber;  
  47.                         options.Method = "GET";  
  48.                         options.FallbackMethod = "GET";  
  49.                         options.StatusCallbackMethod = "GET";  
  50.   
  51.                         // call  
  52.                         var callResponse = client.InitiateOutboundCall(options);  
  53.                         if (!object.Equals(callResponse.Sid, null))  
  54.                         {  
  55.                              response = Request.CreateResponse(HttpStatusCode.OK,"Please wait for call for your verification code");  
  56.                         }  
  57.                         else  
  58.                         {  
  59.                             response = Request.CreateResponse(HttpStatusCode.NotFound, "");  
  60.                         }  
  61.                     }  
  62.                   
  63.                 }  
  64.   
  65.               
  66.             catch (Exception)  
  67.             {  
  68.              
  69.                 throw;  
  70.             }  
  71.            return response;  
  72.         }  
  73.     } 

Output


The image, shown above, gives the output. I got the call for the user verification. With the help of Twilio service and Web API, we can use the text message Service of Twilio with Web API, as it will come in next article.

Summary

In this article, we learned how to create Verification calling Service, using Web API and Twilio.