OK. I looked at some examples online using RNGCryptoService and couldn't get them to work as I wanted so thought I'd come up with a slightly different approach:-
I wanted something that would just create a random temporary password where a user's has expired.Code:public static string GenerateRandomPassword(int len) { StringBuilder newPwd = new StringBuilder(); Char[] Alpha = "ABCDEFGHJKLMNPQRSTUVWXYZ".ToCharArray(); Char[] alpha = "abcdefghijkmnopqrstuvwxyz".ToCharArray(); Char[] num = "23456789".ToCharArray(); Random random = new Random(); int x; int y=1; for (int i = 0; i < len; i++) { if (i >= len - 2) { x = random.Next(0, (num.Length - 1)); newPwd.Append(num[x]); } else { if (y % 2 == 1) { x = random.Next(0, (Alpha.Length - 1)); newPwd.Append(Alpha[x]); } else { x = random.Next(0, (alpha.Length - 1)); newPwd.Append(alpha[x]); } } y++; } return newPwd.ToString(); }
It'll produce a password based on the length passed as a parameter, with the last 2 characters as digits and the rest of as alternating upper and lower case letters. It excludes potentially ambiguous characters like O,0, 1, l, I.
Hope someone finds it of use.



LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks