Random password generator using Ajax in ASP.Net MVC


You can download the project from here : Download MVCPasswordExample 

As title says when user click a button it send ajax request to controller
$.post('@Url.Content("~/Home/GeneratePassword/")' ...
and returns Json with new 8 character password.


MODEL



public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
 



VIEW


@{
    ViewBag.Title = "Home Page";
}

@using MVCPasswordExample.Models
@model RegisterModel

<script>
    $(document).ready(function () {
        $('#updateprofile').click(function () {
            $('form')[0].reset();

        });

        $('#autogenerate').click(function () {
            $.post('@Url.Content("~/Home/GeneratePassword/")', function (data) {

                $('#password, #confirmpassword').attr("value", data.password);

            });
        });

    });

</script>


<h2>@ViewBag.Message</h2>

<table style="margin-top:100px;width:40%">
 <tr>
 <th colspan="3">Change Password</th>
 </tr>


  <tr>
      <td>@Html.LabelFor(m => m.Password)
      </td>
     <td style="width:20%">
         @Html.TextBox("password", null, new { @class = "search_field", @id = "password", @Value = "" })
     </td>
      <td><input type="button" id="autogenerate" value="Auto generate" class="submit_button" /></td>
 </tr>
    
  <tr>
      <td>
         @Html.LabelFor(m => m.ConfirmPassword)
      </td>
     <td class="confirm" colspan="2">
          @Html.TextBox("confirmpassword", null, new { @class = "search_field", @id = "confirmpassword" })
      </td>

 </tr>


 </table>


CONTROLLER

        [HttpPost]
        public JsonResult GeneratePassword()
        {
           var pass = Guid.NewGuid().ToString().Substring(0, 8);


            return Json(new { password = pass });

        }

You can download the project from here : Download MVCPasswordExample




Comments

  1. Cool! I think we had this discussion before.

    The problem with your code is that you are missing the fact that substrings of GUIDS are not unique. This is an issue because you are dealing with passwords thought. What happens here is that you will come up with duplicated values (which should not be a recommend scenario for a password).

    I wrote a blog post about this issue here http://blog.michaelhidalgo.info/2013/08/a-poc-to-verify-that-guids-substrings.html

    While I understand that this is a cool demo on how to use Ajax and ASP.NET MVC, I think it is worth to make that password stronger. Or at least to put a note on why substrings of GUIDS are not unique.

    So others readers can see a potential issue with it.

    Thanks!

    ReplyDelete
  2. This solution is easy to use, requiring no special knowledge and not bombarding you with complex settings or confusing configurations and controls.reset windows 10 password

    ReplyDelete
  3. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.



    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete

Post a Comment