2017-09-22 24 views
1

我正在開發一個使用C#的Web應用程序,該應用程序在註冊期間使用2因素身份驗證。我已經使用Nexmo的API嘗試了2FA。它運行良好。我所要做的就是打電話給他們的API並指定'to'號碼。這裏是代碼:Twilio Authy 2FA for OneCode C#實現

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中指定的'to'號碼。我是一名初學者,使用.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的,請幫我配置在C#。

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

當你運行這段代碼時會發生什麼?任何錯誤?你從API獲得什麼迴應? –

+0

我無法運行此..它說無法找到'AuthyAPIKey' –

+0

這是什麼說的呢? – Andy

回答

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 
    } 

讓我知道這是否有幫助。