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

Random Generate Password in ASP.NET

Article:
Viewed:  989 
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.

1.png

Image 1.

2.png

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Generate Random Password</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="2" cellspacing="2" width="40%" align="center" border="2">
            <tr>
                <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"
                        BackColor="AliceBlue"></asp:Label>
                    <br />
                    <br />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 

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);
    }
}

  Comment:
 
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