【问题标题】:2-Step Authentication in ASP.Net MVCASP.Net MVC 中的两步验证
【发布时间】:2011-05-24 18:45:53
【问题描述】:

我正在开发一个使用 FormsAuthentication 和自定义 MembershipProvider 的 ASP.Net Mvc 3 应用程序(因此我确实可以控制提供程序返回的内容)。

要求要求一个两步验证过程(用户名和密码,然后是秘密问题)。如果不通过这两个步骤,用户应该无法访问网站的任何“安全”部分。请不要提及这是否是多因素安全性,我已经知道了。

请就如何最好地完成此任务提供建议。

以下是一些注意事项:

  • 我被允许(在架构上)使用会话 - 我不想这样做。
  • 对于提供安全内容的控制器,我更愿意使用开箱即用的 [Authorize] ActionFilter
  • 负责人希望两个步骤的 url 相同:即www.contoso.com/login/。至少在我的尝试中,当用户在第二步中输入错误的答案时(他们没有正式登录,但我需要确保我仍然在反对半-经过身份验证的用户的秘密问题/答案)。

谢谢。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 forms-authentication


    【解决方案1】:

    将自定义视图模型与隐藏的表单字段结合使用。只要确保这一切都是通过 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; }
    }
    

    操作方法

    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>
    

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

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

    【讨论】:

    • 很抱歉,如果问题超过 1 个,我只是想提供有关约束的最详细信息。这绝对解决了我的问题,谢谢!
    【解决方案2】:

    我可能会做一些第一步让他们输入用户名和密码的事情。检查它,如果它很好,将它们移动到授权标记视图,要求他们输入问题的答案。如果他们没有做到这一点,就把他们签出来,把他们赶出去,不管怎样。我认为这在一个视图中是不可能的,除非您呈现部分视图并且如果他们没有完成身份验证过程就离开了,您将他们注销并清除他们的 cookie。

    ----编辑----- 再三考虑,你可以做一个部分视图,只是在他们完成部分视图的第二部分之前不要让他们登录。一些伪代码:

    public ActionResult Login(){
      get username and password off the view
      if its valid
         render a partial view that asks for the secret answer
         if thats valid
           forms auth login
         else
           try again, get booted, or whatever
       else
          get booted, try again, whatever
    }
    

    【讨论】:

    • 我知道这是伪代码,但我无法渲染部分视图然后检查它在同一流程中是否有效。另外,用户名和密码是否一定存在于表单上? (是的,如果它是第 1 步,但第 2 步呢?)。最后,我知道该方法声明了ActionResult 的返回,但是这意味着返回代表用户名/密码的ViewResult 和代表安全问题的PartialResult 是什么意思?
    • 好点 - 看这里:stackoverflow.com/questions/5437745/… 你也许可以利用它
    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    相关资源
    最近更新 更多