Introduction:-
In this article , I have explained how to generate random
password in asp.net using c#. This is very simple to generate random password
using Random function of c#. You are thinking what is need to generate random
password . You have seen in many website when you register in website they have
asked to enter your email_id and then
after they send user name and password in your email because it is tuff work to
generate password manully.
Now open Visual studio -> open new website .
On design page put
three labels.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Mixed Pass: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
Lower Pass: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
Upper Pass: <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Code behind page:-
protected void Page_Load(object
sender, EventArgs e)
{
Label1.Text =
LowerOrUpperCasePassGen();
Label2.Text = LowerCasePass();
Label3.Text = UpperCasePass();
}
protected Random ran
= new Random();
protected string[] strCharacters = { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9","0",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
// mixed pass gen
or combination with lower and uppercase
public string LowerOrUpperCasePassGen()
{
int p =
0;
string
pass = string.Empty;
Random
ran = new Random();
for (int x = 0; x < 8; x++)
{
p = ran.Next(0, 61);
pass += strCharacters[p];
}
return
pass;
}
//for only Upper
case pass gen.
public string UpperCasePass()
{
int p =
0;
string
pass = string.Empty;
Random
ran = new Random();
for (int x = 0; x < 8; x++)
{
p = ran.Next(0, 35);
pass += strCharacters[p];
}
return
pass;
}
//for only lower
case pass gen.
public string LowerCasePass()
{
int p =
0;
string
pass = string.Empty;
Random
ran = new Random();
for (int x = 0; x < 8; x++)
{
p = ran.Next(26, 61);
pass += strCharacters[p];
}
return
pass;
}
In this code you can generate random password for 8
character or digit or mixed. If You want to generate random password greater
than 8 then change the digit in for loop.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.