Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Verify C# ASP.NET Core Quickstart


With just a few lines of code, your C# ASP.NET application can verify phone numbers and add an additional layer of security with Twilio Verify.

This C# Verify Quickstart will teach you how to do this using our Verify REST API, the Twilio C# helper library, and C# ASP.NET Core(link takes you to an external page) to ease development.

In this Quickstart, you will learn how to:

  1. Sign up for Twilio
  2. Set up your development environment
  3. Send your first SMS phone verification
  4. Check verification codes
(information)

Info

Short on time? Spin up a low-code, fully editable verification demo in less than 2 minutes using Twilio's Code Exchange and Quick Deploy here(link takes you to an external page).


Sign up for Twilio

sign-up-for-twilio page anchor

If you already have a Twilio account, you're all set here! Feel free to jump to the next step.

Before you can send an SMS from C#, you'll need to sign up for a Twilio account(link takes you to an external page) or sign into your existing account.

You can sign up for a free Twilio trial account here(link takes you to an external page).

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console(link takes you to an external page) . This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.

Do I need a phone number?

do-i-need-a-phone-number page anchor

If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.

Verify uses Services for configuration. To send a Verify API request you will need both your Twilio Credentials and a Service SID. You can create and update a Service in two ways:

  1. In the Verify Console(link takes you to an external page)
  2. With the Verify API

Services can be used to edit the name (which shows up in the message template), set the code length (4-10 characters), enable settings like the "do not share warning" and more.

Now that you have a Twilio account and a verification service, you can start writing some code! To make things even easier we will use Twilio's official helper for C# and .NET applications(link takes you to an external page).


If you've gone through one of our other .NET Core Quickstarts already and have .NET Core installed, you can skip this step and get straight to sending your first verification.

To start a phone verification and send your first SMS, you'll need to have the .NET Core SDK installed - if you don't know if you have the .NET Core installed, run the following command to see what version you have:

dotnet --version

You should see something similar to this output:

2.X.XXX

The Twilio SDK requires .NET Framework version 3.5 or higher or any .NET runtime supporting .NET Standard v1.4(link takes you to an external page).

If you have a not supported version of .NET Core or no .NET Core at all, you'll need to install the .NET Core SDK before going any further. Follow the directions for installing the .NET Core SDK for your platform (Windows, Mac, Linux) from the Microsoft .NET Download Page(link takes you to an external page).

Should you prefer to use .NET Framework instead of .NET Core the instructions for building and running the project will be different and you will need to change the targeted framework. Besides that, the code should work straight away.

Send an SMS verification code

send-an-sms-verification-code page anchor

Sends a one-time passcode to a user's phone number

C#

_27
// Install the C# / .NET helper library from twilio.com/docs/csharp/install
_27
_27
using System;
_27
using Twilio;
_27
using Twilio.Rest.Verify.V2.Service;
_27
_27
_27
class Program
_27
{
_27
static void Main(string[] args)
_27
{
_27
// Find your Account SID and Auth Token at twilio.com/console
_27
// and set the environment variables. See http://twil.io/secure
_27
string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
_27
string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
_27
_27
TwilioClient.Init(accountSid, authToken);
_27
_27
var verification = VerificationResource.Create(
_27
to: "+15017122661",
_27
channel: "sms",
_27
pathServiceSid: "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_27
);
_27
_27
Console.WriteLine(verification.Status);
_27
}
_27
}

Output

_23
{
_23
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"to": "+15017122661",
_23
"channel": "sms",
_23
"status": "pending",
_23
"valid": false,
_23
"date_created": "2015-07-30T20:00:00Z",
_23
"date_updated": "2015-07-30T20:00:00Z",
_23
"lookup": {},
_23
"amount": null,
_23
"payee": null,
_23
"send_code_attempts": [
_23
{
_23
"time": "2015-07-30T20:00:00Z",
_23
"channel": "SMS",
_23
"attempt_sid": "VLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}
_23
],
_23
"sna": null,
_23
"url": "https://verify.twilio.com/v2/Services/VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Verifications/VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}

Check a verification code

check-a-verification-code page anchor

Checks the one-time passcode sent to the user. The provided code is correct if the response 'status' parameter is 'approved'.

C#

_27
// Install the C# / .NET helper library from twilio.com/docs/csharp/install
_27
_27
using System;
_27
using Twilio;
_27
using Twilio.Rest.Verify.V2.Service;
_27
_27
_27
class Program
_27
{
_27
static void Main(string[] args)
_27
{
_27
// Find your Account SID and Auth Token at twilio.com/console
_27
// and set the environment variables. See http://twil.io/secure
_27
string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
_27
string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
_27
_27
TwilioClient.Init(accountSid, authToken);
_27
_27
var verificationCheck = VerificationCheckResource.Create(
_27
to: "+15017122661",
_27
code: "123456",
_27
pathServiceSid: "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_27
);
_27
_27
Console.WriteLine(verificationCheck.Status);
_27
}
_27
}

Output

_14
{
_14
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"to": "+15017122661",
_14
"channel": "sms",
_14
"status": "approved",
_14
"valid": true,
_14
"amount": null,
_14
"payee": null,
_14
"sna_attempts_error_codes": [],
_14
"date_created": "2015-07-30T20:00:00Z",
_14
"date_updated": "2015-07-30T20:00:00Z"
_14
}


Clone and Setup the Verification Application

clone-and-setup-the-verification-application page anchor

Start by cloning our C# ASP.NET Core repository.(link takes you to an external page)


_10
git clone git@github.com:TwilioDevEd/verify-v2-quickstart-csharp.git

If you don't have git installed or prefer to download the source code you can grab a zip file of the project here(link takes you to an external page).

Install project dependencies

install-project-dependencies page anchor

Twilio C# .NET Helper Library is already in the dependencies of the project. Building the project will download them.


_10
cd verify-v2-quickstart-csharp/VerifyV2Quickstart/
_10
_10
dotnet build

Create a new file twilio.json and update the content. It will store your Twilio Account sensitive data. You can find your Account SID and Auth Token in the Twilio Console(link takes you to an external page). Create a new Verification Service in the console(link takes you to an external page) and add the Verification SID to your twilio.json file.


_10
{
_10
"Twilio": {
_10
"AccountSid": "Your Twilio Account SID",
_10
"AuthToken": "Your Twilio Auth Token",
_10
"VerificationSid": "Your Verify Service SID"
_10
}
_10
}

Run the application


_10
dotnet ef database update
_10
_10
dotnet run

If your credentials are set up correctly you'll soon get a message that the app is up!


Use the .NET Core Twilio Verify Demo

use-the-net-core-twilio-verify-demo page anchor

Navigate to http://localhost:5000/Identity/Account/Register(link takes you to an external page). You should see a registration form that looks like this:

sign up form with phone verification.

Enter your phone number and choose which channel to request verification over. Finally hit the green Sign Up button and wait. You'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (the call will tell you to enter a number on the phone keypad).

Enter the token into the Verification entry form and click 'Verify':

verification entry form.

And with that, your demo app is protected with Twilio's Phone Verification!


Your demo app is now keeping fraudulent users from registering with your business and polluting your database. Next, check out all of the variables and options available to you in the Verify API Reference.

After that, check out adding additional verification channels supported by the Verify API like:

(information)

Info

Lastly, to protect your service against fraud, view our guidance on Preventing Toll Fraud when using Verify.


Rate this page: