No need to write it again... the question says it all.
Asked
Active
Viewed 1.2k times
4 Answers
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
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();
}