Twilio Authy 2FA for OneCode C# Implementation Twilio Authy 2FA for OneCode C# Implementation curl curl

Twilio Authy 2FA for OneCode C# Implementation


Twilio developer evangelist here.

When making a call to the API, you do need to add the X-Authy-API-Key header as well as a URL parameter api_key. Also, to start the process of verifying a number you should be making a POST request with the data you need to send to the API.

The two bits of data that you need are the phone number and the country code for that phone number. Though you can set some other values, like the way you want to send the verification code (via sms or call).

I would update your code to look like this:

public static async Task StartVerifyPhoneAsync()  {    // Create client    var client = new HttpClient();    var AuthyAPIKey = 'YOUR AUTHY API KEY';    // Add authentication header    client.DefaultRequestHeaders.Add("X-Authy-API-Key", AuthyAPIKey);    var values = new Dictionary<string, string>    {      { "phone_number", "PHONE NUMBER TO VERIFY" },      { "country_code", "COUNTRY CODE FOR PHONE NUMBER" }    };    var content = new FormUrlEncodedContent(values);    var url = $"https://api.authy.com/protected/json/phones/verification/start?api_key={AuthyAPIKey}";    HttpResponseMessage response = await client.PostAsync(url, content);    // do something with the response  }

Then when the user enters the code, you need to check it. Again you should add the API key as a header and send as a URL parameter too, along with the phone number, country code and the verification code the user entered, this time as a GET request.

public static async Task CheckVerifyPhoneAsync()  {    // Create client    var client = new HttpClient();    var AuthyAPIKey = 'YOUR AUTHY API KEY';    // Add authentication header    client.DefaultRequestHeaders.Add("X-Authy-API-Key", AuthyAPIKey);    var phone_number = "PHONE NUMBER TO VERIFY";    var country_code = "COUNTRY CODE FOR PHONE NUMBER";    var verification_code = "THE CODE ENTERED BY THE USER";    var url = $"https://api.authy.com/protected/json/phones/verification/start?api_key={AuthyAPIKey}&phone_number={phone_number}&country_code={country_code}&verification_code={verification_code}";    HttpResponseMessage response = await client.GetAsync(url);    // do something with the response  }

Let me know if that helps at all.