我正在使用 C# 开发一个 Web 应用程序,该应用程序在注册期间使用两因素身份验证。我已经使用 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 的 cURL 实现 here ,请帮我用C#配置一下。
curl "http://api.authy.com/protected/json/users/new?api_key=d57d919d11e6b221c9bf6f7c882028f9" \
-d user[email]="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1b4b2a4b381a5aeaca0a8afefa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>" \
-d user[cellphone]="317-338-9302" \
-d user[country_code]="54"
最佳答案
这里是 Twilio 开发者布道者。
调用 API 时,您需要添加 X-Authy-API-Key
header 以及 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 。不过您可以设置一些其他值,例如您想要发送验证码的方式(通过
短信或电话)。
我会将您的代码更新为如下所示:
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 key 作为 header ,并作为 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
}
请告诉我这是否有帮助。
关于c# - 用于 OneCode C# 实现的 Twilio Authy 2FA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368870/