Generating random password in bulk
I use this source code for generating random passwords :
public string GetRandomPasswordUsingGUID(int length)
{
// Get the GUID
string guidResult = System.Guid.NewGuid().ToString();
// Remove the hyphens
guidResult = guidResult.Replace("-", string.Empty);
// Make sure length is valid
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
// Return the first le开发者_运维知识库ngth bytes
return guidResult.Substring(0, length).ToUpper();
}
It works fine when you invoke the method ,But not in "for" loop statement .
At this case it generate some repeated password which is wrong .
for example like this :
A4MNB597D7
AMGJCCC902
AWJ80CF6HX
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A2LYJCH23N
A2LYJCH23N
How can i create random password in "For" loop statement ?
GUIDs are not random, they are only unique (within a single system). Even a random number generator has limits on it, the minimum and maximum values it will return, and being truly random means you could get the same result over and over again, you just can't tell.
Are you sure you mean random, as opposed to strong?
http://xkcd.com/221/Ok, so now we have some idea of what you want 500 -1000 unique passwords. I'd question the need for uniqueness, as I would presume that they're for a user account, however ... (entered without VS handy)
List<string> passwords = new List<string>();
while (passwords.Length < 1000)
{
string generated = System.Web.Security.Membership.GeneratePassword(
10, // maximum length
3) // number of non-ASCII characters.
if (!passwords.Contains(generated))
passwords.Add(generated);
}
And then you'll have a list of 1000 unique passwords, which have a maximum of 10 characters, and 3 non-ASCII characters.
This is not an answer to the question specifically, but it is why your GUID solution will not work:
http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx
If you're going to generate random passwords in build I would strongly recommend not using "NewGuid()" because based on the generation algorithm for creating the UUIDs segments of them are based on a unique ~100ms timestamp.
Look at:
http://en.wikipedia.org/wiki/Universally_unique_identifier
You would be better off creating a look-up table of allowed characters and using a static "Random" object and indexing characters into the table based on the random number generated.
Ironically, you'd have had better results if you used the last characters of your GUID rather than the first.
To answer your question, something like this would suffice:
private static Random rng=new Random();
private static string PasswordAlphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public string GetRandomPasswordUsingGUID(int length)
{
string result="";
while(length-->0)
result+=PasswordAlphabet[rng.Next(PasswordAlphabet.Length)];
return result;
}
You could use Asp.net's Membership class, which has a password generator built in. It's in the System.Web.Security namespace in the System.Web dll.
// Generate a new 12-character password with 1 non-alphanumeric character.
string password = Membership.GeneratePassword(12, 1);
More details here on MSDN: Membership.GeneratePassword Method
精彩评论