How to autogenerate random password in asp.net mvc application?
No n开发者_开发百科eed to write it again... the question says it all.
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.
Here's a nice article that might help you.
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...
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();
}
精彩评论