ASP.Net MVC - 停止自动完成密码

标签 asp.net asp.net-mvc

我有一个现有的 asp.net mvc web 应用程序,我想阻止浏览器下次自动记住密码。我可以在不需要更改密码上的输入 autocomplete="off"的情况下执行此操作,因为我可以看到这应该可以解决 future 的问题,任何已经完成此操作的人可能仍然可以登录,因为密码已缓存。

最佳答案

确实autocomplete="off"会做的伎俩。你会看到它也在 SO 中使用(右上角):

<input style="display: inline-block; width: 188px; max-width: 188px;" name="q" placeholder="search" value="" tabindex="1" autocomplete="off" maxlength="240" type="text">

您可以使用 Razor 将其添加到您的输入中:
@Html.TextBoxFor(model => model.Input, new { autocomplete="off" })

您还可以使用 jQuery 一次性定位所有这些:
$(document).ready(function() {
    $("input:text, form").attr("autocomplete", "off");
})

对于缓存部分,请查看 "How to Turn Off Form Autocompletion" (MDN) 中的摘录(整个文件值得一读):

Setting autocomplete="off" here has two effects:

  • it stops the browser saving field data for later autocompletion on similar forms though heuristics that vary by browser.
  • it stops the browser caching form data in session history. When form data is cached in session history, the information the user has filled in will be visible after the user has submitted the form and clicked on the Back button to go back to the original form page.

In some case, the browser will keep suggesting autocompletion values even if the autocomplete attribute is set to off. This unexpected behavior can be quite puzzling for developers. The trick to really force the no-completion is to assign a random string to the attribute like so :

autocomplete="nope"

Since this random value is not a valid one, the browser will give up.



您还应该检查所有主要浏览器之间的这种行为是否一致(取决于您所追求的浏览器支持)。

更新 :Michael Liu 在评论中指出“现代浏览器忽略密码字段的自动完成属性”,在这种情况下,我不知道合法的解决方案。

您可以尝试放置一个额外的 input字段类型 password在实际的顶部并将其设置为隐藏,浏览器应尝试自动完成:
<!-- before your actual password field -->
<input style="display: none;" type="password" name="pwdplaceholder"/>

...然而,它扰乱了浏览器行为,这不是最佳实践。

也看看another SO question (由 Michael Liu 在评论中分享)。

关于ASP.Net MVC - 停止自动完成密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34996209/

相关文章:

ASP.NET 控件事件处理程序未在回发时触发?

jquery - 将所有文本框集成到一个 `KeyUp` 事件中

c# - 扩展 NHibernate DriverConnectionProvider 类

asp.net-mvc - ASP.NET MVC Html.RadioButton 异常

jquery - MVC 4 - 带 ID 和标签的 jQuery 自动完成,保存子对象

asp.net-mvc - ICommandHandler/IQueryHandler与异步/等待

asp.net - JavaScript 是否允许在点击事件期间调用远程网页?

c# - wpf 中两个不同选项卡中的两个 AutoCompleteBox 控件

asp.net - 遍历结果集.Net

c# - 如何导出 .xlsx 文件 C# ASP.NET MVC razor