用于OneCode C#实现的Twilio Authy 2FA [英] Twilio Authy 2FA for OneCode C# Implementation

查看:160
本文介绍了用于OneCode C#实现的Twilio Authy 2FA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发一个Web应用程序,该Web应用程序在注册期间使用2因子身份验证。我已经使用Nexmo的API尝试了2FA。工作正常。我所要做的就是调用他们的API,并指定收件人号码。下面是代码:

I am developing a web application using C# that uses 2 factor authentication during Sign Up. I have already tried 2FA using Nexmo's API. It worked fine. All I had to do was call their API and speciy the 'to' number. Here is the code:

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。但是我不知道应该在哪里输入Nexmo中指定的收件人号码。我是初学者,正在使用.NET(C#)代码段。这是代码片段。请帮助我像在Nexmo中一样配置此代码。

Now, I decided to give Twilio a try. I came across Authy and it's process. I found their 2FA API here. But I don't understand where should I enter the 'to' number as specified in Nexmo. I am a beginner and using .NET(C#) code snippet. Here is the code snippet. Please help me configure this code as I am able to do in 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 此处,请帮助我在C#中对其进行配置。

They have given a cURL implementation of their api here, please help me configure it in 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"


推荐答案

此处是Twilio开发人员的福音。

Twilio developer evangelist here.

调用API时,您确实还需要添加 X-Authy-API-Key 标头作为网址参数 api_key 。另外,要开始验证数字的过程,您应该使用需要发送到API的数据发出POST请求。

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
  }

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

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.

这篇关于用于OneCode C#实现的Twilio Authy 2FA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆