简体   繁体   English

ASP.NET MVC密码检查:弱,正常,强

[英]Asp.net mvc password check: Weak, Normal, Strong

I wanna make a password check for the webshop that im building for school. 我想为学校为学校建造的网上商店进行密码检查。 Thats indicated with color alert: so red for weak, orange, for normal, and green foor good. 多数民众赞成在颜色警报表示:红色代表弱,橙色,代表正常,绿色代表良好。 What could be the best approach to do this? 最好的方法是什么?

Here is a little section of where the user need to put in a password: 这是用户需要输入密码的一小部分:

        password
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Klant.password)
        @Html.ValidationMessageFor(model => model.Klant.password)
    </div>

This is a homework assignment, and we are informed we're not allowed to use javascript. 这是一项家庭作业,我们被告知我们不允许使用javascript。

The best approach would definitely be to do this on the client using javascript. 最好的方法肯定是使用javascript在客户端上执行此操作。 I wrote a jQuery function for you which does the following: 我为您编写了一个jQuery函数,该函数执行以下操作:

  1. Executes every time a key press occurs in your password text box 每次在您的密码文本框中发生按键操作时执行
  2. Evaluates the text length 评估文字长度
  3. Changes the background colour of a div based on password length 根据密码长度更改div的背景颜色

Note: Simply download jQuery(if you don't have it already in the Scripts folder) and add a reference to it in your view and make sure the id of the password text box is correct(I've called mine "Password") 注意:只需下载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>
}   

If you don't come right I've created a sample project and uploaded it to Google drive .Just click File->Download to get the .zip file 如果您输入的不正确,我已经创建了一个示例项目并将其上传到Google驱动器 。只需单击File-> Download以获取.zip文件

You can use following javascript code for checking strength: 您可以使用以下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;
}

Apart from this you can refer following links: 除此之外,您还可以参考以下链接:

http://passwordadvisor.com/CodeAspNet.aspx http://passwordadvisor.com/CodeAspNet.aspx

http://www.c-sharpcorner.com/uploadfile/f9935e/password-policystrength-Asp-Net-mvc-validator/ http://www.c-sharpcorner.com/uploadfile/f9935e/password-policystrength-Asp-Net-mvc-validator/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM