469,963 Members | 1,241 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

generate randaom password(1 uuper case letter, 1 digit and 6 character length)

Hi,

I want generate random password. it should contain at least 1 upper case letter, 1 digit, 6 character length.

I can use Membership.GeneratePassword(-, -), but its not having numbers some times.

Any help please?

Thank you.
Jul 16 '10 #1
1 5378
Hi Ramanak,

Here's a quick algorithm to do want you want to do. Sorry it's not the cleanest but it works. Didn't have plenty of time to test it though.

You can change the minimum and maximum number of chars. You can also add other chars (symbols, etc) if you want. You can also make it cleaner, but don't create the Random object in the function or it will always generate the same password.

Again, sorry it's not the cleanest but I think you get the idea.

Have a good day.

Tomy


Expand|Select|Wrap|Line Numbers
  1.         protected String RandomPassword()
  2.         {
  3.             String pwd = "";
  4.             int length = 0;
  5.             int index = 0;
  6.             int numericIndex = -1;
  7.             int upperCaseIndex = -1;
  8.  
  9.             //Length of your password
  10.             length = rnd.Next(MINLENGTH, MAXLENGTH);
  11.  
  12.             // You generate a password of the desired length
  13.             for (int i = 0; i < length; i++)
  14.             {
  15.                 // Generate an index that smaller than the size of your allowed chars
  16.                 index = rnd.Next(0, allowedChars.Length);
  17.  
  18.                 pwd += allowedChars[index];
  19.             }
  20.  
  21.             ////*********************************************************
  22.             // We make sure that there is at least one numeric
  23.             // Replace one random char by a numeric
  24.             numericIndex = rnd.Next(0, pwd.Length);
  25.  
  26.             // Generate a numeric, delete one char and replace it with a numeric
  27.             index = rnd.Next(0, numericChars.Length);
  28.             pwd = pwd.Remove(numericIndex, 1);
  29.             pwd = pwd.Insert(numericIndex, numericChars[index].ToString());
  30.             ////*********************************************************
  31.  
  32.  
  33.             ////*********************************************************
  34.             // We make sure that there is at least one uppercase
  35.             // Replace one random char by a numeric
  36.             upperCaseIndex = rnd.Next(0, pwd.Length);
  37.  
  38.             // We make sure our uppercase index is different
  39.             // from our numeric index or we will overwrite our
  40.             // only numeric value with our uppercase value
  41.             while (upperCaseIndex == numericIndex)
  42.             {
  43.                 upperCaseIndex = rnd.Next(0, pwd.Length);
  44.             }
  45.  
  46.             // Generate a numeric, delete one char and replace it with a numeric
  47.             index = rnd.Next(0, upperCaseChars.Length);
  48.             pwd = pwd.Remove(upperCaseIndex, 1);
  49.             pwd = pwd.Insert(upperCaseIndex, upperCaseChars[index].ToString());
  50.             ////*********************************************************
  51.  
  52.             return pwd;
  53.         }
  54.  
  55.         Random rnd = new Random();
  56.         const int MINLENGTH = 6;
  57.         const int MAXLENGTH = 20;
  58.         const String allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  59.         const String upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  60.         const String numericChars = "0123456789";
  61.  
Jul 16 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Adams-Blake Co. | last post: by
4 posts views Thread by avillela | last post: by
1 post views Thread by bigd7788 | last post: by
4 posts views Thread by Chris | last post: by
3 posts views Thread by Army1987 | last post: by
reply views Thread by Jonathan Boivin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.