2

I used Membership.GeneratePassword(10, 0) in my C# application code. The GeneratePassword method Generates a random password of the specified length.

The second argument specifies the the minimum number of non-alphanumeric characters (such as @, #, !, %, &, and so on) in the generated password.

I don't want any non-alphanumeric characters so I specified zero. However, it still fails because I got a password with Nonalphanumeric content when I execute Membership.GeneratePassword(10, 0)

Lg|1-F;?a4

Why the pipe( | ) and the hypen( - ) and semicolon and the question mark?

crazyTech
  • 1,379
  • 3
  • 32
  • 67
  • 4
    Unfortunately, `GeneratePassword` specifies a minimum ,not maximum number of non alpha characters. You'll need to roll your own - see [here](http://stackoverflow.com/questions/2625150/membership-generate-password-alphanumeric-only-password) – StuartLC Sep 12 '12 at 14:50
  • Why are non-alphanumeric characters an issue for you? – Damien_The_Unbeliever Sep 12 '12 at 14:53
  • Some of the users that use the web application have low-level computer skills. They don't know what key to press for pipe( | ) ? – crazyTech Sep 12 '12 at 14:55
  • @Damien_The_Unbeliever Some of the users that use the web application have low-level computer skills. They don't know what key to press for pipe( | ) – crazyTech Sep 12 '12 at 14:57

1 Answers1

0

I ended up using this workaround below, which uses regex to strip out non-alphanumeric characters and replaces them with a random number.

String newPassword = Membership.GeneratePassword(8, 0); //Second arg only guarantees a minimum number (not maximum) number of alphanumeric characters.
Random rng = New Random();
newPassword = Regex.Replace(newPassword, @"[^a-zA-Z0-9]", rng.Next(0, 10).ToString());
BassGod
  • 151
  • 1
  • 11