我使用以下源代码生成随机密码:
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 length bytes
return guidResult.Substring(0, length).ToUpper();
}
当您调用该方法时,它可以正常工作,但在“for”循环语句中却不能。
在这种情况下,它会生成一些重复的密码,这是错误的。
例如:
A4MNB597D7
AMGJCCC902
AWJ80CF6HX
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A2LYJCH23N
A2LYJCH23N
如何在“for”循环语句中创建随机密码?