ASP.NET MVC5 - Password Meter jQuery Plugin Integration

In any product, which is being developed, user interaction plays the most vital role in developing product perception for the target user. Like any other product, Websites that focus on improving both UI/UX is becoming vital and game changing. Improving a Website interactivity can be done with two approaches i.e.

  1. Traditional Approach that involves creating every aspect of UI/UX from hand by yourself i.e. from the graphics assets to jQuery base resources etc. This approach however, requires a lot of time, which is mostly not suited in a fast paced environment.
  2. Plugin Integration Approach is the second approach, which involves an integration of open source or commercially free to use licensed plugins as a component into your own Website by of course tweaking the plugin, according to one's own need, which is not a simple task but, not as time consuming as the traditional approach, but still requires an intelligent effort to mold the open source plugin, according to your choice of platform and need.
    There are a lot of beautiful plugins out there, waiting to be grabbed and make your product (i.e. Website, desktop Application, mobile Application etc.) look as modern, glossy and interactive, as it possibly can be. My focus is to grab them and mold them to be used with C#.NET technology, since, it’s the technology of love for me.

Today, I will be focusing on a Password Meter jQuery based plugin, which will inform the user about the suitable strength of his/her password. What is Password Meter? Password Meter is the ability to indicate the strength of the password, which any Website requires from its registered user to have for security purposes. Password validation is a separate thing, so, do not mix it  up with Password Meter. Password Meter focuses on the complexity of a password i.e. how difficult it is to guess a password.

I will be using a beautiful & a simple jQuery base plugin, created by Mr. Dan Palmer called jQuery Complexify. You can download this plugin here. There is however, a bug in it, and I have fixed that bug.


output

I choose this plugin, simply because of its simplicity and the fact that it also allows the consumers to add a ban list of passwords that they do not want the users to use, while registering for their Website. I did not see this feature in any other plugin so far.

The following are some prerequisites before you proceed any further in this article-

Prerequisites

The prerequisites includes the knowledge about the following technologies-
  1. ASP.NET MVC 5
  2. HTML
  3. JavaScript
  4. Ajax
  5. Bootstrap
  6. JQuery
  7. C# Programming

You can download the complete source code or you can follow the step by step discussion, given below. The sample code is being developed in Microsoft Visual Studio 2015 Enterprise.

Before I jump into my topic, take jQuery Complexify plugin.Let me highlight the bug, which is pertaining in jQuery Complexify plugin. When you download the .js file of jQuery Complexify plugin, open it, navigate to "function inBanlist(str)" and replace the following line of code i.e.

if (str.toLowerCase().indexOf(options.bannedPasswords[i].toLowerCase()) !== -1)

with the following line of code i.e.

if (options.bannedPasswords[i].toLowerCase().indexOf(str.toLowerCase()) !== -1)

If you do not do this change, you will not be able to use the "strict" mode of ban list, as described by the author.

Now, let’s begin.

  1. Create new MVC Web project and name it "PasswordMeterJq".
  2. Create new folder, name it "Plugins" and place the downloaded plugin files in this new folder and incorporate its related JavaScript files into the project.
  3. Open "HomeController.cs" file and replace with the code, given below-
    1. using System;    
    2. using System.Collections.Generic;    
    3. using System.Linq;    
    4. using System.Web;    
    5. using System.Web.Mvc;    
    6. namespace PasswordMeterJq.Controllers    
    7. {    
    8.   public class HomeController : Controller    
    9.   {    
    10.     public ActionResult Index()    
    11.     {    
    12.       return View();    
    13.     }    
    14.   }    
    15. }   
    In the code, given above, I have cleaned the existing file and create "Index()" method, which simply returns my view.

  4. In "Model" folder, create "PasswordMeterJq.cs" file and place the code, given below-
    1. using System.Collections.Generic;    
    2. using System.ComponentModel.DataAnnotations;    
    3. namespace PasswordMeterJq.Models    
    4. {    
    5.   public class PasswordViewModel    
    6.   {    
    7.     [Required]    
    8.     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]    
    9.     [DataType(DataType.Password)]    
    10.     [Display(Name = "Password")]    
    11.     public string Password { get; set; }    
    12.   }    
    13. }    
    In the code, given above, I have created a simple model for my password component.

  5. Now, in "Content" folder, create a folder "files", create a file and name it "banlist.txt". You can add the passwords in this file that you do not want your user to use, while they register on your Website e.g. 123456, password etc. In the file you have created, place following sample passwords that will be banned i.e.

    password|1234567890


    Follow the above format, when you add your banned passwords.

  6. Now, open the "_Layout.cshtml" in "Views" folder and replace it with the code, given below-
    1. <!DOCTYPE html>    
    2. <html>    
    3. <head>    
    4.   <meta charset="utf-8" />    
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">    
    6.   <title>@ViewBag.Title</title>    
    7.   @Styles.Render("~/Content/css")    
    8.   @Scripts.Render("~/bundles/modernizr")    
    9.   <!-- Font Awesome -->    
    10.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />    
    11. </head>    
    12. <body>    
    13.   <div class="navbar navbar-inverse navbar-fixed-top">    
    14.     <div class="container">    
    15.       <div class="navbar-header">    
    16.         <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">    
    17.           <span class="icon-bar"></span>    
    18.           <span class="icon-bar"></span>    
    19.           <span class="icon-bar"></span>    
    20.         </button>    
    21.       </div>    
    22.     </div>    
    23.   </div>    
    24.   <div class="container body-content">    
    25.     @RenderBody()    
    26.     <hr />    
    27.     <footer>    
    28.       <center>    
    29.         <p><strong>Copyright © @DateTime.Now.Year - <a href="http://asmak9.blogspot.com/">Asma's Blog</a>.</strong> All rights reserved.</p>    
    30.       </center>    
    31.     </footer>    
    32.   </div>    
    33.   @Scripts.Render("~/bundles/jquery")    
    34.   @Scripts.Render("~/bundles/bootstrap")    
    35.   <!-- Password Complexify -->    
    36.   @Scripts.Render("~/plugin/password-complexify")    
    37.   @Scripts.Render("~/scripts/custom-password-complexify")    
    38.   @RenderSection("scripts", required: false)    
    39. </body>    
    40. </html>   
    In the code, given above, I have simply incorporated required JavaScript files and mold the layout, according to my choice.

  7. Now, in "Views\Home" folder create "Index.cshtml" file and place the code, given below-
    1. @using PasswordMeterJq.Models    
    2. @model PasswordViewModel    
    3. @{    
    4.   ViewBag.Title = "ASP.NET MVC5: Jquery Password Plugin";    
    5. }    
    6. <h2>@ViewBag.Title.</h2>    
    7. <div class="row">    
    8.   <div class="col-md-12">    
    9.     <section id="loginForm">    
    10.       @using (Html.BeginForm("Index""Home"new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))    
    11.       {    
    12.         @Html.AntiForgeryToken()    
    13.         <h5>    
    14.           <i class="fa fa-link" aria-hidden="true"></i>    
    15.           <a href="https://danpalmer.me/jquery-complexify/">Complexify Password Meter</a>    
    16.         </h5>    
    17.         <hr />    
    18.         <div class="form-group">    
    19.           <div id="complexify">    
    20.             <div class="form-group">    
    21.               @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })    
    22.               <div class="col-md-4">    
    23.                 @Html.PasswordFor(m => m.Password, new { id = "PassBox", @class = "form-control" })    
    24.                 @Html.ValidationMessageFor(m => m.Password, ""new { @class = "text-danger" })    
    25.               </div>    
    26.               <div class="col-md-2 col-md-pull-2" style="margin-left: 7% !important;margin-top: 0.4% !important;">    
    27.                 <div class="progress text-center">    
    28.                   <div id="complexity-bar" class="progress-bar" role="progressbar">    
    29.                     <span id="complexity" class="progress-value" style="position:absolute;right:0;left:0;color:black !important">0% </span>    
    30.                   </div>    
    31.                 </div>    
    32.               </div>    
    33.             </div>    
    34.           </div>    
    35.         </div>    
    36.         @*<div class="form-group">    
    37.           <div class="col-md-offset-2 col-md-10">    
    38.             <input type="submit" value="Log in" class="btn btn-default" />    
    39.           </div>    
    40.         </div>*@    
    41.       }    
    42.     </section>    
    43.   </div>    
    44. </div>    
    45. @section Scripts    
    46.  {    
    47.   @Scripts.Render("~/bundles/jqueryval")    
    48. }   
    In the code, given above, I have simply created a password box, attached with my model and a progress bar, which will be indicating the user about his/her password strength.

  8. Under "Scripts" folder, create a file "custom-complexify.js" and place the code, given below, in it-
    1. $(document).ready(function ()    
    2. {    
    3.   jQuery.get('Content/files/banlist.txt'function (data)    
    4.   {    
    5.     // Settings.    
    6.     var banlist = data.split('|');    
    7.     $("#complexify #PassBox").complexify(    
    8.     {    
    9.       minimumChars: 6,    
    10.       strengthScaleFactor: 0.3,    
    11.       bannedPasswords: banlist,    
    12.       banMode: 'strict'    
    13.     },    
    14.     function callback(valid, complexity)    
    15.     {    
    16.       // Progress bar.    
    17.       var progressBar = $('#complexify #complexity-bar');    
    18.       progressBar.toggleClass('progress-bar-success', valid);    
    19.       progressBar.toggleClass('progress-bar-danger', !valid);    
    20.       progressBar.css({ 'width': complexity + '%' });    
    21.       $('#complexify #complexity').text(Math.round(complexity) + '%');    
    22.     });    
    23.   });    
    24. });   

This is where the actual action is happening. In the code, given above, I am first extracting my ban list passwords from the "banlist.txt" file , followed by applying the "jQuery Complexify" plugin. I have also set the basic properties and a callback function, which will update the progress bar base on the complexity indication, I received.

Let’s look into the properties of the plugin i.e. for the following properties and pass the input password as “pass" i.e.

  1. minimumChars: 6,   
  2. strengthScaleFactor: 0.3,   
  3. bannedPasswords: banlist,   
  4. banMode: 'strict'   
It will get the indicating meter, given below-

output

Notice, I am using "strict" ban mode. Therefore, any subset or password itself from the ban list will return "0" indicating password is not valid.

Now, let’s change the properties and follow the input password as "pass" i.e.

banMode: 'loose'

It will get the indicating meter, given below-

output

Since, I am using "loose" ban mode, therefore only the password itself from the ban list will return "0", thereby indicating the password is not valid but the subset of the ban password will rather return the measure of its strength depending upon our scale factor and minimum character.

Let’s play with the scale factor property now, which changes the scale factor to following and my input password as "bbbbbb" i.e.
  1. minimumChars: 6,   
  2. strengthScaleFactor: 1,   
  3. bannedPasswords: banlist,   
  4. banMode: 'strict'   
It will return the indicator, given below-

output

Notice, by default, I set scale factor to be "0.3". The reason is that I want to accommodate 6 character length password into it, which will give following indication i.e.
output

The scale factor ranges from 0 above, higher scale factor means higher strength of the password.

Let’s change minimum character length as follows and pass the input character equal to 20 character length, given below-
  1. minimumChars: 20,    
  2.        strengthScaleFactor: 1,    
  3.        bannedPasswords: banlist,    
  4.        banMode: 'strict'   
It will give the indication, given below-

output

Conclusion

In this article, you learned  how to use Jquery Complexify Password Meter plugin with ASP.NET MVC5 Web Application. You will also learn advanced usage of this plugin and how you can easily mold this plugin, as per your own requirement by simply playing around with the properties. You will also be able to learn to create a progress bar indicator, which is being manipulated by the jQuery Complexify plugin.


Similar Articles