Monday, March 12, 2018

Generate random password in MVC application


In this article I am going to explain how to generate random password in MVC application.


Description:
I want to generate strong random password for users when they registers to website. Here in this article I am going to generate password using 2 ways.


Implementation:
Add an empty controller to project. I have create action Generatepassword.

  public ActionResult Generatepassword()
        {
            return View();
        }

Using Membership Generate method

This is the built-in function. To use this add the namespace to controller System.Web.Security. To generate the password you have to specify the length (number of charters) and number of special charters (non-alphanumeric).  In this example 8 is the length of password and 1 will be special chartaters.
        [HttpPost]
        public ActionResult GeneratePassword()
        {
            string password = Membership.GeneratePassword(8, 1);
            ViewBag.password = password;
            return View("Generatepassword ");
        }


Using loop (alphanumeric + non-alphanumeric charters)

[
HttpPost]
        public ActionResult GenerateAlphaNumericPwd()
        {
            string numbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()-=";
            Random objrandom = new Random();
            string passwordString = "";
            string strrandom = string.Empty;
            for (int i = 0; i < 8; i++)
            {
                int temp = objrandom.Next(0, numbers.Length);
                passwordString = numbers.ToCharArray()[temp].ToString();
                strrandom += passwordString;
            }
            ViewBag.strongpwd = strrandom;
            return View("Generatepassword ");
        }


Complete code of controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using infinitescroll.Models;
using System.Web.Security;

namespace infinitescroll.Controllers
{
    public class testController : Controller
    {
        //
        // GET: /test/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Generatepassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GenerateAlphaNumericPwd()
        {
            string numbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()-=";
            Random objrandom = new Random();
            string passwordString = "";
            string strrandom = string.Empty;
            for (int i = 0; i < 8; i++)
            {
                int temp = objrandom.Next(0, numbers.Length);
                passwordString = numbers.ToCharArray()[temp].ToString();
                strrandom += passwordString;
            }
            ViewBag.strongpwd = strrandom;
            return View("Generatepassword ");
        }

        [HttpPost]
        public ActionResult GeneratePassword()
        {
            string password = Membership.GeneratePassword(8, 1);
            ViewBag.password = password;
            return View("Generatepassword ");
        }
        //
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}


 Add view

Add view to Generatepassword actionresult.  

Complete source of view:

@{
    ViewBag.Title = "Generate Password";
}

@using (Html.BeginForm("GenerateAlphaNumericPwd", "Test"))
{
    <table>
        <tr>
            <td>Click on button to Generate Random Strong Password :</td>
            <td><input id="btnpwd" type="submit" value="Generate Password" /></td>
        </tr>
        <tr>
            <td></td>
            <td>@ViewBag.strongpwd</td>
        </tr>
    </table>
}

@using (Html.BeginForm("GeneratePassword", "Test"))
{
    <table>
        <tr>
            <td>Click on button to Generate Password (Membership):</td>
            <td><input id="btnpwd" type="submit" value="Generate Password" /></td>
        </tr>
        <tr>
            <td></td>
            <td>@ViewBag.password</td>
        </tr>
    </table>
}





No comments:

Post a Comment