【问题标题】:Twilio Authy 2FA for OneCode C# Implementation用于 OneCode C# 实现的 Twilio Authy 2FA
【发布时间】:2017-09-22 16:03:47
【问题描述】:

我正在使用 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#) 代码 sn-p。这是代码sn-p。请帮助我配置此代码,就像我在 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"

【问题讨论】:

  • 运行这段代码会发生什么?有什么错误吗?您从 API 得到什么响应?
  • 我无法运行它。它说找不到AuthyAPIKey
  • 这是什么意思?
  • 你从哪里得到AuthyAPIKey?如果是这样有什么重要性?您是否从某处复制粘贴代码?你看过这个 API 的文档了吗?
  • AuthyAPIKey 在我发布的 api 代码中作为函数 client.DefaultRequestHeaders.Add("X-Authy-API-Key", AuthyAPIKey); 的参数存在。我假设我必须用我拥有的 api 密钥替换字符串,但 AuthyAPIKey 仍然给出找不到它的错误。另外我应该在哪里输入收件人的号码?

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


【解决方案1】:

这里是 Twilio 开发者宣传员。

调用 API 时,您确实需要添加 X-Authy-API-Key 标头以及 URL 参数 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
  }

让我知道这是否有帮助。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2016-02-24
    • 2021-10-31
    • 2023-01-20
    相关资源
    最近更新 更多