Twilio Authy 2FA用于OneCode C#实现

时间:2017-09-22 16:03:48

标签: c# asp.net .net curl twilio

我正在使用C#开发一个Web应用程序,它在注册期间使用2因素身份验证。我已经使用Nexmo的API尝试了2FA。它工作正常。我所要做的只是调用他们的API并指定'到'号码。这是代码:

public ActionResult Start(string to)
{
    var start = NumberVerify.Verify(new NumberVerify.VerifyRequest
    {
        number = to,
        brand = "NexmoQS"
    });
    Session["requestID"] = start.request_id;

    return View();
}

现在,我决定试试Twilio。我遇到了Authy,这是一个过程。我找到了他们的2FA API here。但我不明白我应该在哪里输入Nexmo中指定的'到'号码。我是初学者并使用.NET(C#)代码片段。这是代码片段。请帮我配置这段代码,因为我可以在Nexmo中完成。

public static async Task VerifyPhoneAsync()
  {
    // Create client
    var client = new HttpClient();

    // Add authentication header
    client.DefaultRequestHeaders.Add("X-Authy-API-Key", AuthyAPIKey);

    // https://api.authy.com/protected/$AUTHY_API_FORMAT/phones/verification/check?phone_number=$USER_PHONE&country_code=$USER_COUNTRY&verification_code=$VERIFY_CODE
    HttpResponseMessage response = await client.GetAsync("https://api.authy.com/protected/json/phones/verification/check?phone_number=5558675309&country_code=1&verification_code=3043");

    // Get the response content.
    HttpContent responseContent = response.Content;

    // Get the stream of the content.
      using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
      {
        // Write the output.
        Console.WriteLine(await reader.ReadToEndAsync());
      }
    }

他们已经给出了他们的api here的cURL实现,请帮我在C#中配置它。

curl "http://api.authy.com/protected/json/users/new?api_key=d57d919d11e6b221c9bf6f7c882028f9" \
-d user[email]="user@domain.com" \
-d user[cellphone]="317-338-9302" \
-d user[country_code]="54"

1 个答案:

答案 0 :(得分:2)

Twilio开发者传道者在这里。

调用API时,您需要添加X-Authy-API-Key标头以及网址参数api_key。此外,要开始验证号码的过程,您应该使用您需要发送到API的数据发出POST请求。

The two bits of data that you need are the phone number and the country code for that phone number。虽然您可以设置其他一些值,例如您希望发送验证码的方式(via短信或致电)。

我会将您的代码更新为:

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
  }

然后当用户输入代码时,您需要检查它。您应该再次添加API密钥作为标题,并将其作为URL参数以及用户输入的电话号码,国家/地区代码和验证码发送,这次是作为GET请求。

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
  }

让我知道这是否有帮助。