加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 03:41:54 所属栏目:asp.Net 来源:网络整理
导读:我正在使用带有自定义MembershipProvider的FormsAuthentication开发一个ASP.Net Mvc 3应用程序(所以我确实可以控制提供程序返回的内容). 这些要求要求执行两步验证过程(用户名和密码后跟秘密问题).如果不通过这两个步骤,用户就无法访问网站的任何“安全”部
我正在使用带有自定义MembershipProvider的FormsAuthentication开发一个ASP.Net Mvc 3应用程序(所以我确实可以控制提供程序返回的内容).

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

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

以下是一些注意事项:

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

谢谢.

解决方法

将自定义视图模型与隐藏表单字段结合使用.只需确保通过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或类似的东西进行测试.

顺便说一句,这个问题看起来好像是一些问题……所以刚刚回答了如何用一种视图/动作方法进行两步验证.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读