asp.net-mvc - ASP.Net MVC 中的两步验证

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

我正在使用 FormsAuthentication 处理 ASP.Net Mvc 3 应用程序带定制 MembershipProvider (所以我确实可以控制提供者返回的内容)。

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

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

以下是一些注意事项:

  • 我被允许(在架构上)使用 session - 不想这样做。
  • 我更愿意使用开箱即用的 [Authorize] ActionFilter对于提供安全内容的 Controller 。
  • 负责人希望 2 个步骤的 url 相同:即 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/

    相关文章:

    asp.net-mvc - 在 ASP.NET Core 中创建单元测试

    css - 对 MVC3 ActionLink 进行主题化,以便所有链接具有一致的宽度

    asp.net-mvc - 使用 ASP.NET MVC 查询字符串中的身份验证 token 对用户进行身份验证

    ASP.Net FormsAuthentication Redirect 丢失 Redirect 和 Application_AuthenticateRequest 之间的 cookie

    c# - 枚举到模型 C# MVC4 中的复选框

    javascript - Jquery 日期选择器本地化

    c# - 从数据库中检索用户并转换为用户对象

    Jquery ajax 随机错误仅在 Chrome 中出现

    .net - VS 热重载在 NET Core 5 中不起作用

    c# - 单击 HTML 按钮(不是提交或表单的发布按钮)调用 Controller 的特定操作 Asp.net MVC