Friday, 28 June 2013

How To Auto Generate Random Password or Random number or string in Asp.net using C#





Description:-

                    In this example we explain that how to Auto generate Unique password or Generating Random number or Random String with proper validation Like Two alpha Character,Two Numeric Character,Two Special character Like generate a strong password.

for Ex:-
       
          in this example once user click on Button the generatepassword() function is called and password will be display in Label.

We all know that today in this Internet world in every website when we click on the ForgotPassword Link it will Send a Auto Generate Unique Password in our Email Id or our Mobile Number as per site choice. So Devloping this type of Functionality in our Application you have to use this code for password Generation.

             The main advantage of this program is we can Generate Random and Unique Password in Proper way so this type of Facility is very Useful in when user click on Forgot password and we can send a Random Unique Password to His/Her Email Id. 

How to Send Forgot Password to user Email Send Forgot Password to user email

Rotate the Ads witout Refreshing the webpage Rotate the Advertisement in Asp.Net

Export Gridview Data to PDF file Export Grid data to PDF File




passwordgenerate.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="passwordgenerate.aspx.cs" Inherits="WebApplication1.passwordgenerate" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>





passwordgenerate.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

namespace WebApplication1
{
    public partial class passwordgenerate : System.Web.UI.Page
    {
        // string chk = "k";
        string NewPassword = "";
        string IDString = "";
        string temp = "";
        int tmp;
        //int s = 0;

        List<int> generated = new List<int>();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = GeneratePassword(8);

        }


        public int Next()
        {
            Random r1 = new Random();

            do { tmp = r1.Next(0, 8); }
            while (generated.Contains(tmp));
            generated.Add(tmp);
            return tmp;
        }

        public string GeneratePassword(int PasswordLength)
        {

            string allowedChars0 = "1,2,3,4,5";
            const string allowedChars1 = "6,7,8,9,0";

            const string allowedChars2 = "A,B,C,D,E,F,G,H,I,J,K,L,M";
            const string allowedChars3 = "N,O,P,Q,R,S,T,U,V,W,X,Y,Z";

            const string allowedChars4 = "a,b,c,d,e,f,g,h,i,j,k,l,m";
            const string allowedChars5 = "n,o,p,q,r,s,t,u,v,w,x,y,z";

            const string allowedChars6 = "!,@,#,$,%";
            const string allowedChars7 = "&,*,+,?";

            char[] sep = { ',' };

            Random rand = new Random();

            for (int i = 0; i < PasswordLength; i++)
            {
                Next();

                // chk += tmp.ToString();

                switch (tmp)
                {


                    case 0:
                        {

                            string[] arr = allowedChars0.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 1:
                        {
                            string[] arr = allowedChars1.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 2:
                        {
                            string[] arr = allowedChars2.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 3:
                        {
                            string[] arr = allowedChars3.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 4:
                        {
                            string[] arr = allowedChars4.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 5:
                        {
                            string[] arr = allowedChars5.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 6:
                        {
                            string[] arr = allowedChars6.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }
                    case 7:
                        {
                            string[] arr = allowedChars7.Split(sep);
                            temp = arr[rand.Next(0, arr.Length)];
                            IDString = temp;
                            NewPassword += IDString;
                            break;
                        }

                }
            }

            return NewPassword;

        }

    }

}

0 comments:

Post a Comment