7

No need to write it again... the question says it all.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132

4 Answers4

15

You can use the built-in function included in the namespace System.Web.Security.

Membership.GeneratePassword Method
Generates a random password of the specified length.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
7

Here's a nice article that might help you.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

In the past I've done it once by using a piece of a Guid. I just created a new guid, converted it to a string and took the piece I wanted, I think I used the characters in the back, or the other way around. Tested it with 100 loops and every time the string was different.

Doesn't has anything to do with MVC though...

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Jan_V
  • 4,244
  • 1
  • 40
  • 64
0
 public string CreatePassword(int length)
    {
        const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder res = new StringBuilder();
        Random rnd = new Random();
        while (0 < length--)
        {
            res.Append(valid[rnd.Next(valid.Length)]);
        }
        return res.ToString();
    }