Mos*_*afa 4 c# asp.net 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 length bytes
return guidResult.Substring(0, length).ToUpper();
}
Run Code Online (Sandbox Code Playgroud)
它在调用方法时工作正常,但不在"for"循环语句中.
在这种情况下,它会生成一些错误的重复密码.
例如像这样:
A4MNB597D7
AMGJCCC902
AWJ80CF6HX
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A2LYJCH23N
A2LYJCH23N
Run Code Online (Sandbox Code Playgroud)
如何在"For"循环语句中创建随机密码?
GUID不是随机的,它们只是唯一的(在单个系统中).即使一个随机数生成器对它有限制,它将返回的最小值和最大值,并且真正随机意味着你可以一遍又一遍地得到相同的结果,你只是无法分辨.
你确定你的意思是随机的,而不是强者吗?
好的,现在我们已经知道你想要500-1000个唯一密码.我会质疑是否需要唯一性,因为我认为它们是用户帐户,但是......(没有VS方便输入)
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);
}
Run Code Online (Sandbox Code Playgroud)
然后,您将拥有1000个唯一密码的列表,其中最多包含10个字符和3个非ASCII字符.
| 归档时间: |
|
| 查看次数: |
1278 次 |
| 最近记录: |