Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Random Generate Password in ASP.NET

Viewed:  1108 
Posted On:  25/02/2013 21:04:59 
How we can generate random password in ASP.NET 

In this article we are going to learn how we can generate password randomly.


Image 1.


Image 2.

ASPX of this application is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Generate Random Password</title>
    <form id="form1" runat="server">
        <table cellpadding="2" cellspacing="2" width="40%" align="center" border="2">
                <td align="center">
                    <asp:Button ID="btnGeneratePassword" runat="server" Text="Click To Generate Password"
                        OnClick="btnGeneratePassword_Click" />             
<br />
                    <br />
                    <asp:Label ID="lblPwd" runat="server" Font-Bold="True" Font-Italic="True" Font-Names="Arial"
                    <br />
                    <br />

aspx.cs code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void btnGeneratePassword_Click(object sender, EventArgs e)
        lblPwd.Text = GenerateRandomPassword(8);
    public static string GenerateRandomPassword(int PwdLength)
        string _allowedChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
        Random randNum = new Random();
        char[] chars = new char[PwdLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < PwdLength; i++)
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        return new string(chars);

By Narendar  On  07/02/2016 23:32:33
how many can generate using above string "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ" and when repeated occur
By Rahul Saxena  On  25/02/2013 22:20:13
Good One... Very Helpfull..
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us