登录我的应用时,这适用于2FA。 我的控制器中有两个公共异步任务方法。第一个效果很好。除非用户的电话号码与我在数据库中的电话号码不匹配,否则没有错误。 这是方法:
[HttpPost]
public async Task<ActionResult> StartVerifyPhoneAsync(AuthyModel authyModel)
{
string mobileNumber = authyModel.MobileNumber.Replace(@"-", "");
if(mobileNumber != Session["MobileNumber"].ToString())
{
Session["AuthyError"] = "Invalid Phone Number";
return View();
}
// Create client
var client = new HttpClient();
// Add authentication header
client.DefaultRequestHeaders.Add("X-Authy-API-Key", AuthKey);
var values = new Dictionary<string, string>
{
{ "via", "sms" },
{"phone_number", mobileNumber },
{"country_code", "1" },
{"code_length", "6" }
};
var content = new FormUrlEncodedContent(values);
var url = $"https://api.authy.com/protected/json/phones/verification/start?api_key=" + AuthKey;
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
return View("VerificationCode");
}
else
{
Session["AuthyErrors"] = response.ReasonPhrase;
return View("Index");
}
}
在我的下一个方法中,我完全按照这个问题Twilio Authy
中的答案说然而,它返回和未经授权的状态代码,401:
[HttpGet]
public async Task<ActionResult> CheckVerifyPhoneAsync(AuthyModel authyModel)
{
if(Session["VerifyAttempt"] == null || Session["VerifyAttempt"].ToString() == "")
{
Session["VerifyAttempt"] = "1";
}
int verifyAttempt = int.Parse(Session["VerifyAttempt"].ToString());
if (verifyAttempt < 3)
{
string mobileNumber = Session["MobileNumber"].ToString();
string code = "";
if(!String.IsNullOrEmpty(authyModel.VerificationCode))
{
code = authyModel.VerificationCode.Trim();
}
// Create client
var client = new HttpClient();
// Add authentication header
client.DefaultRequestHeaders.Add("X-Authy-API-Key", AuthKey);
var phone_number = mobileNumber;
var country_code = "1";
var verification_code = code;
var url = $"https://api.authy.com/protected/json/phones/verification/start?api_key=" + AuthKey + "&phone_number=" + phone_number + "&country_code=" + country_code + "&verification_code=" + verification_code;
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
//removed for stackoverflow view
}
else
{
Session["AuthyErrors"] = response.StatusCode;
Session["VerifyAttempt"] = (int.Parse(Session["VerifyAttempt"].ToString()) + 1).ToString();
return View("VerificationCode");
}
}
else
{
return RedirectToAction("Logout", "Home");
}
}
以下是回复:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
Status: 401 Unauthorized
X-Content-Type-Options: nosniff
Date: Tue, 22 May 2018 18:22:42 GMT
Server: //removed for Stackoverflow
Server: (Ubuntu)
WWW-Authenticate: Basic realm="Access Denied"
Content-Length: 247
Content-Type: application/json; charset=utf-8
}}
非常感谢任何帮助。我无法弄清楚如何通过这个401错误或为什么我甚至得到它。