ASP.Net MVC中的两步认证

Eth*_*iac 6 asp.net-mvc forms-authentication asp.net-mvc-3

我正在使用FormsAuthentication自定义的ASP.Net Mvc 3应用程序MembershipProvider(所以我对提供程序返回的内容有一些控制权).

这些要求要求执行两步验证过程(用户名和密码后跟秘密问题).如果不通过这两个步骤,用户就无法访问网站的任何"安全"部分.我已经知道,请不要提及这是否是多因素安全性.

请提供有关如何最好地完成此任务的建议.

以下是一些注意事项:

  • 我被允许(在架构上)使用会话 - 我不愿意.
  • 我更喜欢使用开箱即[Authorize] ActionFilter用的控制器提供安全内容.
  • 负责人希望两个步骤的网址是相同的:即www.contoso.com/login/.至少在我的尝试中,当用户在第二步中输入错误的答案时(这些问题没有正式登录,但是我需要确保我仍在努力对抗这一半),这会引起一些轻微但不是微不足道的问题.经过身份验证的用户的秘密问题/答案).

谢谢.

Cha*_*ino 4

将自定义视图模型与隐藏表单字段结合使用。只要确保这一切都是通过 https 完成的。

视图模型

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; }
}
Run Code Online (Sandbox Code Playgroud)

行动方法

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);
} 
Run Code Online (Sandbox Code Playgroud)

看法

<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>
Run Code Online (Sandbox Code Playgroud)

如果您不满意将用户名和密码发送回隐藏字段中的视图以重新进行身份验证并确保他们没有作弊...您可以创建一个 HMAC 或类似的东西来测试。

顺便说一句,这个问题似乎是几个问题合二为一......所以只是回答了如何使用一种视图/操作方法进行两步身份验证。