OTP Verification using MSG91 in Xamarin Forms

Now a days OTP verification is a common ways that are usually useful for our mobile user verification.Its a pretty simple and straight forward for authenticate the user with their mobile number.

So in this article you will learn how to send OTP using a SMS provider.Their are lots of SMS provider are available in markets.And remember that all SMS providers are cost effective.Based on your application you can choose your plan for SMS.Here I am using MSG91 for sending OTP SMS to user mobile number.



To create a account on MSG91,go the the site of https://msg91.com/ and register an account.After registering you may receive few trial sms limit,you can use it for implementing SMS sender in your application.You need to pay to MSG91 for larger amount of SMS.

After creating an account you can see your authKey and SenderId,It will needed for sending the SMS.

Next: here I already design a simple screen with a textbox and a button inside our content page-

Before Implementing the code for sending SMS,we need to install two following packages from nuget:


  • RestSharp : For sending HTTP request.
  • NewtonSoftJson : For Serialize(Object to Json Data) and Deserialize(Json Data to Object).
Add three class inside our portable project:
First these two class OTPModel Sms classes are using for sending the payload of that HTTP post request.


And this OTPResponse class is using for getting the response form that HTTP call.











After that I implemented the click event of that Next button inside our code behind page:

private async void SendOTP(object sender, EventArgs e)
        {

            // send a GET request  
            string countryCode = "91";  // two digit only

            OTPModel model = new OTPModel();
            model.sender = "SOCKET";
            model.route = "4";
            model.country = countryCode;
            string message = $"<#> Your OTP is {GenerateOTP()} 89XB%2BseFjklo";
            List numbers = new List { mobileNumber.Text };
            model.sms.Add(new Sms { message = message, to = numbers });

            var client = new RestClient($"https://api.msg91.com/api/v2/sendsms?country={countryCode}");
            var request = new RestRequest(Method.POST);
            request.AddHeader("content-type", "application/json");
            request.AddHeader("authkey", "281817ABVlLRzJt5d0a30e2");
            string jsonData = JsonConvert.SerializeObject(model);
            request.AddParameter("application/json", jsonData, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            if (response.IsSuccessful)
            {
                OTPResponse resp = JsonConvert.DeserializeObject(response.Content);
                if (resp.type == "success")
                {
                    await DisplayAlert("Message", $"An OTP send to {numbers[0]}", "Ok");
                }
                else
                {
                    // handle if sms failed to send
                    await DisplayAlert("Message", resp.message, "Ok");
                }
            }
            else
            {
                DisplayAlert("Message", response.ErrorMessage, "Ok");
            }
        }
        public int GenerateOTP()
        {
            return new Random().Next(1000, 9999);
        }


So this code will responsible for sending the OTP message to the number.Remember that,the authKey & SenderId should be valid otherwise message will not sent.

It is not necessary to use MSG91 for sending message,you can use any of SMS provider available in markets.Its depends on you.All SMS providers have their API for sending the sms.you have to read the docs for their API and integrate this inside your project.

To watch the video,follow this link
Sending OTP for verification


OTP Verification using MSG91 in Xamarin Forms OTP Verification using MSG91 in Xamarin Forms Reviewed by SP Tutorials on December 10, 2019 Rating: 5

No comments:

Powered by Blogger.