我正在使用 FormsAuthentication
处理 ASP.Net Mvc 3 应用程序带定制 MembershipProvider
(所以我确实可以控制提供者返回的内容)。
要求强制执行两步身份验证过程(用户名和密码后跟 secret 问题)。如果不通过这两个步骤,用户将无法访问网站的任何“安全”部分。请不要提及这是否是多因素安全性,我已经知道了。
请提供有关如何最好地完成此任务的建议。
以下是一些注意事项:
[Authorize]
ActionFilter
对于提供安全内容的 Controller 。 www.contoso.com/login/
.至少在我的尝试中,当用户在第二步中输入错误答案时,这会导致一些轻微但并非无关紧要的问题(他们没有正式登录,但我需要确保我仍然在针对半经过身份验证的用户的 secret 问题/答案)。 谢谢。
最佳答案
将自定义 View 模型与隐藏表单字段结合使用。只要确保这一切都是通过 https 完成的。
View 模型
public LoginForm
{
public string UserName { get; set; }
public string Password { get; set; }
public int SecretQuestionId { get; set; }
public string SecretQuestion { get; set; }
public string SecretQuestionAnswer { get; set; }
}
操作方法
public ActionResult Login()
{
var form = new LoginForm();
return View(form);
}
[HttpPost]
public ActionResult Login(LoginForm form)
{
if (form.SecretQuestionId == 0)
{
//This means that they've posted the first half - Username and Password
var user = AccountRepository.GetUser(form.UserName, form.Password);
if (user != null)
{
//Get a new secret question
var secretQuestion = AccountRepository.GetRandomSecretQuestion(user.Id);
form.SecretQuestionId = secretQuestion.Id;
form.SecretQuestion = secretQuestion.QuestionText;
}
}
else
{
//This means that they've posted from the second half - Secret Question
//Re-authenticate with the hidden field values
var user = AccountRepository.GetUser(form.UserName, form.Password);
if (user != null)
{
if (AccountService.CheckSecretQuestion(form.SecretQuestionId, form.SecretQuestionAnswer))
{
//This means they should be authenticated and logged in
//Do a redirect here (after logging them in)
}
}
}
return View(form);
}
查看
<form>
@if (Model.SecretQuestionId == 0) {
//Display input for @Model.UserName
//Display input for @Model.Password
}
else {
//Display hidden input for @Model.UserName
//Display hidden input for @Model.Password
//Display hidden input for @Model.SecretQuestionId
//Display @Model.SecretQuestion as text
//Display input for @Model.SecretQuestionAnswer
}
</form>
如果您不满意将用户名和密码发送回隐藏字段中的 View 以重新验证并确保它们没有作弊……您可以创建一个 HMAC 或类似的东西来测试。
顺便说一句,这个问题似乎是几个问题合二为一……所以只回答了如何使用一种 View /操作方法进行两步验证。
关于asp.net-mvc - ASP.Net MVC 中的两步验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6115282/