Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Account Verification with Authy, C#, and ASP.NET MVC

Ready to implement user account verification in your application? Here's how it works at a high level:

  1. The user begins the registration process by entering their data, including a phone number, into a signup form.
  2. The authentication system sends a one-time password to the user's mobile phone to verify the possession of that phone number.
  3. The user enters the one-time password into a form before completing registration.
  4. The user opens a success page and receives an SMS indicating that their account has been created.

Building Blocks

To get this done, you'll be working with the following Twilio-powered APIs:

Authy REST API

  • Authy Docs: Find quick starts, documentation, and all about the helper libraries.

Twilio REST API

  • Messages Resource: We will use Twilio directly to send our user a confirmation message after they have created an account.
        
        
        
        
        AccountVerification.Web/App_Start/IdentityConfig.cs

        Identity Configuration

        AccountVerification.Web/App_Start/IdentityConfig.cs

        All of this can be done in under a half an hour with the simplicity and power of Authy and Twilio.

        Click here to get started!!

        The User Model

        The User Model for this use-case is pretty straight-forward. If you have already read through the 2FA tutorial this one probably looks very similar.

              
              
              
              
              AccountVerification.Web/Models/IdentityModels.cs

              Identity Model

              AccountVerification.Web/Models/IdentityModels.cs

              Pretty simple ApplicationUser model, right? Next, we're going to visit the registration form on the client side.

              Visit the client-side registration form

              Registration Form

              When we create a new user, we ask for a name, e-mail address, password and mobile number with a country code. Using Authy, we send a one-time password to this phone number via SMS to validate it.

              It is now the controller's responsibility to verify that the user has provided the necessary information to create a new user. If the user is created successfully, they will be logged into the system automatically, leaving them logged in but not yet verified.

                    
                    
                    
                    
                    AccountVerification.Web/Controllers/AccountController.cs

                    Register a new user with the account controller

                    AccountVerification.Web/Controllers/AccountController.cs

                    You've now seen how to register an application user. Next, let's take a minute to configure Authy use in our application.

                    Configure your application to work with Authy

                    Authy Configuration

                    Before we move on, let's take a quick look at some application configuration.

                    You need to create a Local.config file under the AccountVerification.Web directory with the following content:

                    <?xml version="1.0" encoding="utf-8"?>
                    <appSettings>
                        <add key="TwilioAccountSID" value="your_twilio_account_SID" />
                        <add key="TwilioAuthToken" value="your_twilio_auth_token" />
                        <add key="TwilioNumber" value="your_twilio_number" />
                        <add key="AuthyKey" value="your_authy_key" />
                    </appSettings>
                     

                    Now we need our Authy production key (sign up for Authy here). When you create an Authy application, the production key can be found on the dashboard.

                    Authy dashboard

                          
                          
                          
                          
                          AccountVerification.Web/Web.config

                          Configure application to work with Twilio and Authy

                          AccountVerification.Web/Web.config

                          This configuration is pretty straightforward, right? Next, we need to jump over to the UserController to set up the Authy client and create an instance method to send a one-time password.

                          Next: Verify a user's account via SMS

                          Sending a Token on Account Creation

                          Once the user has an authyId, we can send a verification code to that user's mobile phone.

                          When our user is created successfully via the form we implemented, we send an SMS with a token to the user's phone to verify their account in our controller. When the code is sent we redirect to another page where the user can enter the token they received, therefore completing the verification process.

                                
                                
                                
                                
                                AccountVerification.Web/App_Start/IdentityConfig.cs

                                Send user an Authy token on account creation

                                AccountVerification.Web/App_Start/IdentityConfig.cs

                                Next, we'll learn how to verify the code the user has provided us during this step.

                                Verify your user's code

                                Verifying the Code

                                The Authy .NET client provides us with a VerifyToken() method that takes a userId and a token as arguments. In this case, we just need to check that the API request was successful and, if so, call setPhoneNumberConfirmedAsync passing true.

                                      
                                      
                                      
                                      
                                      AccountVerification.Web/App_Start/IdentityConfig.cs

                                      Verify an Authy code

                                      AccountVerification.Web/App_Start/IdentityConfig.cs

                                      However, our verification form wouldn't be very usable if there wasn't a way to resend a verification code if the message didn't arrive at the end user's handset.

                                      See how to resend verification codes

                                      Re-sending the Verification Code

                                      Since the form for re-sending the verification code is only one line, we're going to skip that for this tutorial. Let's just look at the controller function.

                                      This controller loads the model associated with the request and then uses the same Authy API method we used earlier to resend the code.

                                            
                                            
                                            
                                            
                                            AccountVerification.Web/Controllers/AccountController.cs

                                            Resend a verification code via .Net controller

                                            AccountVerification.Web/Controllers/AccountController.cs

                                            We're getting close now! Let's provide a pleasant user onboarding experience and send a confirmation message to our new user.

                                            Wrap it up with a nice confirmation message

                                            Sending the Confirmation Message

                                            We create a single instance of the Twilio REST API helper, called twilio in this example.

                                            Then all we need to do to send an SMS to the user's phone is use the built in SendMessage().

                                            For more information on how to send SMS using Twilio's Rest API visit this link.

                                                  
                                                  
                                                  
                                                  
                                                  AccountVerification.Web/App_Start/IdentityConfig.cs

                                                  Send verified user a confirmation message with Twilio

                                                  AccountVerification.Web/App_Start/IdentityConfig.cs

                                                  Congtratulations! You've just implemented account verification so your users can confirm their phone number.

                                                  Where to next?

                                                  Where to Next?

                                                  If you're a C# developer working with Twilio, you might want to check out these other tutorials:

                                                  Click-To-Call

                                                  Put a button on your web page that connects visitors to live support or salespeople via telephone.

                                                  Automated Survey

                                                  Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.

                                                  Did this help?

                                                  Thanks for checking this tutorial out! If you have any feedback to share with us, or just want to let us know what awesome thing you're building, we'd love to hear it. Reach out to us on Twitter!

                                                  Kat King

                                                  Need some help?

                                                  We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.