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.
- public class Verification
-
- {
-
- public string MobileNumber { get; set; }
-
- public string VerificationCode { get; set; }
-
- }
Afterwards, create method inside Twilio controller with the name VerificationCall.
- public class TwilioController : ApiController
- {
- [HttpPost]
- [ActionName("VerificationCall")]
- public HttpResponseMessage VerificationCall(Verification verification)
- {
- HttpResponseMessage response = new HttpResponseMessage();
-
- string accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"].ToString();
- string authToken = ConfigurationManager.AppSettings["TwilioAuthKey"].ToString();
-
- string twilioNumber = ConfigurationManager.AppSettings["TwilioNumber"].ToString();
- try
- {
- var client = new Twilio.TwilioRestClient(accountSid, authToken);
- if (string.IsNullOrEmpty(verification.MobileNumber))
- {
- response = Request.CreateResponse(HttpStatusCode.NotFound, "wrong number");
- }
- else
- {
- Regex regex = new Regex(@"^[0-9]*$");
- string mobileno = string.Empty;
- if (verification.MobileNumber.Contains("-"))
- {
- string[] number = verification.MobileNumber.Split('-');
- mobileno = number[1];
- }
- Match m = regex.Match(verification.MobileNumber);
-
- verification.VerificationCode = string.Empty;
- Random random = new Random();
- string pinCode = (random.Next() % 90000 + 10000).ToString();
-
- verification.VerificationCode = pinCode.ToString();
-
-
- string message = "Welcome%20to%20Atul%20Your%20authorization%20code%20is%20" + HttpUtility.UrlPathEncode(String.Join<char>(" ", pinCode.ToString())) + "&";
-
- string content = "http://twimlets.com/message?Message%5B0%5D=" + message + "";
-
-
- var options = new Twilio.CallOptions();
- options.Url = content;
- options.From = twilioNumber;
- options.To = ConfigurationManager.AppSettings["CountryCode"] + verification.MobileNumber;
- options.Method = "GET";
- options.FallbackMethod = "GET";
- options.StatusCallbackMethod = "GET";
-
-
- var callResponse = client.InitiateOutboundCall(options);
- if (!object.Equals(callResponse.Sid, null))
- {
- response = Request.CreateResponse(HttpStatusCode.OK,"Please wait for call for your verification code");
- }
- else
- {
- response = Request.CreateResponse(HttpStatusCode.NotFound, "");
- }
- }
-
- }
-
-
- catch (Exception)
- {
-
- throw;
- }
- return response;
- }
- }
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.