简体   繁体   English

用于OneCode C#实现的Twilio Authy 2FA

[英]Twilio Authy 2FA for OneCode C# Implementation

I am developing a web application using C# that uses 2 factor authentication during Sign Up. 我正在开发使用C#的Web应用程序,该应用程序在注册期间使用2因子身份验证。 I have already tried 2FA using Nexmo's API. 我已经使用Nexmo的API尝试过2FA。 It worked fine. 工作正常。 All I had to do was call their API and speciy the 'to' number. 我要做的就是调用他们的API,并指定“收件人”号码。 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();
}

Now, I decided to give Twilio a try. 现在,我决定尝试一下Twilio。 I came across Authy and it's process. 我遇到了Authy,这是过程。 I found their 2FA API here . 我在这里找到了他们的2FA API。 But I don't understand where should I enter the 'to' number as specified in Nexmo. 但是我不知道应该在哪里输入Nexmo中指定的“收件人”号码。 I am a beginner and using .NET(C#) code snippet. 我是初学者,正在使用.NET(C#)代码段。 Here is the code snippet. 这是代码片段。 Please help me configure this code as I am able to do in Nexmo. 请帮助我像在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());
      }
    }

They have given a cURL implementation of their api here , please help me configure it in C#. 他们在此处给出了其api的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"

Twilio developer evangelist here. Twilio开发人员布道者在这里。

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 . 调用API时,您确实需要添加X-Authy-API-Key标头以及URL参数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. 另外,要开始验证数字的过程,您应该使用需要发送到API的数据进行POST请求。

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). 尽管您可以设置其他一些值,例如您想要发送验证码的方式( via短信或电话)。

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. 再次,您应该将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
  }

Let me know if that helps at all. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM