Monday 9 November 2020

OTP Verification and Validation Using Twilio

OTP verification combines a user login—including a Password—with physical access to a smartphone or landline telephone to verify authorized access to an Account. A user can begin to log into a secured service by entering an ID and password, but then must receive a one-time code (OTC) or one-time password (OTP) via SMS texting or a voice telephone call using a phone number associated with the account. Entering this additional one-time credential constitutes the second step of verification or the second factor of authentication, with the idea that only someone who knows the correct account password and who physically possesses the required object (Mobile Phone and the associated Phone Number) can gain access to the account.OTP verification are very much essential for 2 Factor authentication which adds extra layer of security.


Step 1 : Sign Up with Twilio
https://www.twilio.com/
Notes :
1. You have to enable Twilio Trail Number
2. You have to copy the AccountSID and AUTH Token and paste it inside your project.

SDK : https://www.twilio.com/docs/libraries/csharp-dotnet/details

MainPage.Xaml


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TwilioDemo.MainPage">
    <StackLayout VerticalOptions="CenterAndExpand" Margin="20,0">
     <Entry Placeholder="Please enter OTP" x:Name="MyOtp"/>
      <Button Text="Login" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Xamarin.Forms;

namespace TwilioDemo
{ // Learn more about making custom code visible in the Xamarin.Forms previewer // by visiting https://aka.ms/xamarinforms-previewer [DesignTimeVisible(false)] public partial class MainPage : ContentPage { string MyrecentOTP; public MainPage() { InitializeComponent(); const string accountSid = "xxxx"; // Your Account SId const string authToken = "xxxxxx"; // Your Account AuthToken TwilioClient.Init(accountSid, authToken); Random generator = new Random(); String randomotp = generator.Next(0, 9999).ToString("D4"); var message = MessageResource.Create( body: "Your new OTP is ?" + randomotp, from: new Twilio.Types.PhoneNumber("+12052368532"), //This is my Trail Number https://www.twilio.com/console to: new Twilio.Types.PhoneNumber("+91739712333") //Add your phone number here ); Console.WriteLine(message.Sid); MyrecentOTP = randomotp; } void Button_Clicked(System.Object sender, System.EventArgs e) { if(MyOtp.Text == MyrecentOTP) { //Go to dashboard } else { App.Current.MainPage.DisplayAlert("Alert","Otp verification Error","Ok"); } } } }

Note :
1.Add countries where you want to give access to.
2.Verify the user phone number [Only on the trail account]
3.ProgrammableSMS>Settings>GeoPermissions
4.Verifying the phone number[Trail Account Only]

No comments:

Post a Comment

All About .NET MAUI

  What’s .NET MAUI? .NET MAUI (.NET Multi-platform App UI) is a framework for building modern, multi-platform, natively compiled iOS, Androi...

Ads2