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?

Masked Phone Numbers with C# and ASP.NET MVC

Download the Code

This ASP.NET sample application is modeled after the amazing rental experience created by AirBnB, but with more Klingons.

Host users can offer rental properties which other guest users can reserve. The guest and the host can then anonymously communicate via a disposable Twilio phone number created just for a reservation. In this tutorial, we'll show you the key bits of code to make this work.

To run this sample app yourself, download the code and follow the instructions on GitHub.

Read how Lyft uses masked phone numbers to let customers securely contact drivers.

Let's get started!

Create a Reservation

The first step in connecting a guest and host is creating a reservation. Here, we handle a form submission for a new reservation which contains the message.

Loading Code Sample...
      
      
      
      
      AirTNG.Web/Controllers/ReservationsController.cs

      Create a Reservation

      AirTNG.Web/Controllers/ReservationsController.cs

      Part of our reservation system is receiving reservation requests from potential renters. However, these reservations need to be confirmed. Let's see how we would handle this step.

      Confirm the Reservation

      Confirm the Reservation

      Before finishing with the reservation, the host needs to confirm that the property was reserved. Learn how to automate this process in our first AirTNG tutorial, Workflow Automation.

      Loading Code Sample...
          
          
          
          
          AirTNG.Web/Controllers/ReservationsController.cs

          Confirm the Reservation

          AirTNG.Web/Controllers/ReservationsController.cs

          Once the reservation is confirmed, we need to purchase a Twilio number that the guest and host can use to communicate.

          Purchase a Twilio Number

          Purchase a Twilio Number

          Here we use a Twilio C# Helper Library to search for and buy a new phone number to associate with the reservation. When we purchase the number, we designate a Twilio Application that will handle webhook requests when the new number receives an incoming call or text.

          We then save the new phone number on our Reservation model, so when our app receives calls or texts to this number, we'll know which reservation the call or text belongs to.

          Loading Code Sample...
              
              
              
              
              AirTNG.Web/Domain/PhoneNumber/Purchaser.cs

              Purchase a Twilio Number

              AirTNG.Web/Domain/PhoneNumber/Purchaser.cs

              Now that each reservation has a Twilio Phone Number, we can see how the application will look up reservations as guest or host calls come in.

              Find a Reservation

              Find a Reservation

              When someone sends an SMS or calls one of the Twilio numbers you have configured, Twilio makes a request to the URL you set in the TwiML app. In this request, Twilio includes some useful information including:

              • The From number that initially called or sent an SMS.
              • The To Twilio number that triggered this request.

              Take a look at Twilio's SMS Documentation and Twilio's Voice Documentation for a full list of the parameters you can use.

              In our controller, we use the to parameter sent by Twilio to find a reservation that has the number we bought stored in it, as this is the number both hosts and guests will call and send SMS to.

              Loading Code Sample...
                  
                  
                  
                  
                  AirTNG.Web/Controllers/PhoneExchangeController.cs

                  Find a Reservation

                  AirTNG.Web/Controllers/PhoneExchangeController.cs

                  Next, let's see how to connect the guest and the host via SMS.

                  Connect Via SMS

                  Connect Via SMS

                  Our Twilio application should be configured to send HTTP requests to this controller method on any incoming text message. Our app responds with TwiML to tell Twilio what to do in response to the message.

                  If the initial message sent to the anonymous number was sent by the host, we forward it on to the guest. Conversely, if the original message was sent by the guest, we forward it to the host.

                  To find the outgoing number we'll use the GatherOutgoingPhoneNumberAsync helper method.

                  Loading Code Sample...
                      
                      
                      
                      
                      AirTNG.Web/Controllers/PhoneExchangeController.cs

                      Connect Via SMS

                      AirTNG.Web/Controllers/PhoneExchangeController.cs

                      Let's see how to connect the guest and the host via phone call next.

                      Connect Via Phone Call

                      Connect Via Phone Call

                      Our Twilio application will send HTTP requests to this method on any incoming voice call. Our app responds with TwiML instructions that tell Twilio to Play an introductory MP3 audio file and then Dial either the guest or host, depending on who initiated the call.

                      Loading Code Sample...
                          
                          
                          
                          
                          AirTNG.Web/Controllers/PhoneExchangeController.cs

                          Connect Via Phone Call

                          AirTNG.Web/Controllers/PhoneExchangeController.cs

                          That's it! We've just implemented anonymous communications that allow your customers to connect while protecting their privacy.

                          Where to Next?

                          Where to Next?

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

                          IVR: Screening and Recording

                          Save time and remove distractions by adding call screening and recording to your IVR (interactive voice response) system

                          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 out this tutorial! If you have any feedback to share with us, we'd love to hear it. Tweet @twilio to let us know what you think.

                          Agustin Camino Andrew Baker  David Prothero

                          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.

                          Loading Code Sample...