[英]Asp.net mvc password check: Weak, Normal, Strong
我想为学校为学校建造的网上商店进行密码检查。 多数民众赞成在颜色警报表示:红色代表弱,橙色,代表正常,绿色代表良好。 最好的方法是什么?
这是用户需要输入密码的一小部分:
password
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Klant.password)
@Html.ValidationMessageFor(model => model.Klant.password)
</div>
这是一项家庭作业,我们被告知我们不允许使用javascript。
最好的方法肯定是使用javascript在客户端上执行此操作。 我为您编写了一个jQuery函数,该函数执行以下操作:
注意:只需下载jQuery(如果您在Scripts文件夹中还没有它),然后在视图中添加对它的引用,并确保密码文本框的ID是正确的(我将其命名为“ Password”)
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
//"Password" is the id of the password textbox
//yours may be different so make sure to change this if necessary
$("#Password").keyup(function () {
var length = $("#Password").val().length;
var colour = "";
if (length <= 4)
colour = "red";
else if (length <= 7)
colour = "orange";
else
colour = "green";
$("#strength").css("background-color", colour);
});
});
</script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
<div id="strength" style="width:100px;height:20px;">
</div>
</div>
<p>
<input type="submit" value="Create" />
</p>
}
如果您输入的不正确,我已经创建了一个示例项目并将其上传到Google驱动器 。只需单击File-> Download以获取.zip文件
您可以使用以下JavaScript代码检查强度:
function pwdStrength(password)
{
var desc = new Array();
desc[0] = "Very Weak";
desc[1] = "Weak";
desc[2] = "Better";
desc[3] = "Medium";
desc[4] = "Strong";
desc[5] = "Strongest";
var score = 0;
//if password bigger than 6 give 1 point
if (password.length > 6) score++;
//if password has both lower and uppercase characters give 1 point
if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
//if password has at least one number give 1 point
if (password.match(/\d+/)) score++;
//if password has at least one special caracther give 1 point
if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
//if password bigger than 12 give another 1 point
if (password.length > 12) score++;
document.getElementById("pwdDescription").innerHTML = desc[score];
document.getElementById("pwdStrength").className = "strength" + score;
}
除此之外,您还可以参考以下链接:
http://passwordadvisor.com/CodeAspNet.aspx
http://www.c-sharpcorner.com/uploadfile/f9935e/password-policystrength-Asp-Net-mvc-validator/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.