ASP.NET - AUTOGENERATE PASSWORD FOR USERS - Asked By Amit D on 13-Mar-09 06:49 AM

I WANT TO GENERATE AUTOGENERATED PASSWORDS FOR NEW USERS.THESE PASSWORDS SHOULD BE ALPHA NUMERIC AND STRONG ENOUGH TO CRACK

PLEASE PROVIDE CLASS/METHOD FOR IT.


Method to generate alpha-numeric unique strings - Kalit Sikka replied to Amit D on 13-Mar-09 06:55 AM

private string GenerateRandomCode()
{
string s = "";
Random random = new Random();
int length = 8;
for (int i = 0; i < length; i++)
{
if (random.Next(0, 3) == 0) //if random.Next() == 0 then we generate a random character
{
s += ((char)random.Next(65, 91)).ToString();
}
else //if random.Next() == 0 then we generate a random digit
{
s += random.Next(0, 9);
}
}
return s;
}

`autogenerate password - mv ark replied to Amit D on 13-Mar-09 07:42 AM

Try this snippet by TATWORTH - http://forums.asp.net/t/1222408.aspx

public static string GetRandomPassword(int iLength)
    {
      if (iLength < 1) iLength = 1;
      if (iLength > 40) iLength = 40;
      String sSeed = Guid.NewGuid().ToString().Replace("-","");      
      return sSeed.Substring(0,iLength);
    }

It takes a Guid, casts it to a string and then it is trimmed to the required length.

Santhosh N replied to Amit D on 13-Mar-09 10:39 AM
there are many ways basically in doing this...and it depends on the requirement
as some need to have strictly a character and a numeric data and some needs one sp char and some need one Cap alphabet...
anyhow here are some snipets...

1)

Random r = new Random();
string password = r.Next().ToString();
if (password.Length > 5)
{
   password = password.Remove(5);
}

2)

private int getRandomNumber(int min, int max)

{return objRandom.Next(min, max + 1);

}

private string generatePassword(int length)

{

int randomNumber;

string strPassword;

string strTemp;

for (int i = 1; i <= length; i++)

{

randomNumber = getRandomNumber(1, 24);

strTemp = (
string)Microsoft.VisualBasic.Interaction.Choose(randomNumber, "B", "1", "D", "2", "F", "3", "G", "4", "H","5", "J", "6", "K", "7", "L", "8", "M", "9", "N", "P", "W", "X", "Y", "Z");

strPassword += strTemp;

}

return strPassword;

}



3)

you can genearte from sessionid

or guid of the system and add some extra string and numerals to these

Re :: AutoGenerate Password for Users - Shailendrasinh Parmar replied to Amit D on 13-Mar-09 01:52 PM

See the following code of generating the alphanumeric ramdon password ::

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
      if (IsPostBack)
      {
            Label1.Text = "Please enter a password length (e.g. 8)";
      }
      TextBox1.Text = "8";
 }

 public static string CreateRandomPassword(int PasswordLength)
 {
  string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
  Random randNum = new Random();
  char[] chars = new char[PasswordLength];
  int allowedCharCount = _allowedChars.Length;

  for (int i = 0; i < PasswordLength; i++)
  {
       chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
  }

  return new string(chars);
 }

 protected void Button1_Click(object sender, EventArgs e)
 {
  if (TextBox1.Text != "")
  {
       string myInt = TextBox1.Text.ToString();
       Label1.Text = "Your generated password is: " + CreateRandomPassword(int.Parse(myInt));
  }
 }
}

Hope this helps.