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应用程序(所以我确实可以控制提供程序返回的内容).
这些要求要求执行两步验证过程(用户名和密码后跟秘密问题).如果不通过这两个步骤,用户就无法访问网站的任何“安全”部分.我已经知道,请不要提及这是否是多因素安全性. 请提供有关如何最好地完成此任务的建议. 以下是一些注意事项: >我被允许(在架构上)使用会话 – 我不愿意. 谢谢. 解决方法
将自定义视图模型与隐藏表单字段结合使用.只需确保通过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或类似的东西进行测试. 顺便说一句,这个问题看起来好像是一些问题……所以刚刚回答了如何用一种视图/动作方法进行两步验证. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在Entity Framework中干净地更新层次结构
- .net – 更好地理解Orchard的’形状’概念
- dependency-injection – asp.net 5依赖注入拦截调用方法
- asp.net-identity – UseOAuthBearerTokens vs UseOAuthBea
- ASP.NET 5中的设计时T4模板(VS 2015)
- asp.net – 无法编写输出文件… pdb visual studio 2010
- ASP.NET Core应用中如何记录和查看日志
- 为什么ASP.NET ListItem类是密封的?
- ASP.NET 5自定义错误页面无法在Azure网站上呈现
- asp.net-mvc-2 – ASP.NET MVC:如何解析url字符串以获取Ro
推荐文章
站长推荐
热点阅读