regex 会员资格生成密码仅字母数字密码?

nc1teljy  于 2022-11-18  发布在  其他
关注(0)|答案(8)|浏览(160)

如何使用Membership.GeneratePassword返回只包含字母或数字字符的密码?默认方法只保证非字母数字密码的最小数量,而不是最大数量。

mjqavswn

mjqavswn1#

string newPassword = Membership.GeneratePassword(15, 0);
newPassword = Regex.Replace(newPassword, @"[^a-zA-Z0-9]", m => "9" );

此正则表达式将用数字字符9替换所有非字母数字字符。

ijxebb2r

ijxebb2r2#

我意识到可能有办法做到这一点。GUID方法很棒,除了它不混合大小写字母。在我的例子中,它只产生小写字母。
所以我决定使用正则表达式来删除非α,然后将结果子串到我需要的长度。

string newPassword = Membership.GeneratePassword(50, 0); 

newPassword = Regex.Replace(newPassword, @"[^a-zA-Z0-9]", m => ""); 

newPassword = newPassword.Substring(0, 10);
mm9b1k5b

mm9b1k5b3#

获取8个字符的字母数字密码的简单方法是生成一个guid并将其用作基础:

string newPwd = Guid.NewGuid().ToString().Substring(0, 8);

如果您需要更长的密码,只需使用子字符串跳过破折号即可:

string newPwd = Guid.NewGuid().ToString().Substring(0, 11);
newPwd = newPwd.Substring(0, 8) + newPwd.Substring(9, 2); // to skip the dash.

如果你想确保第一个字符是字母,你可以在需要的时候用一个固定的字符串来替换它,如果(newPwd[0]〉= '0' && newPwd[0]〈= '9')...
我希望有人能觉得这对你有帮助。:-)

xcitsw88

xcitsw884#

您也可以尝试生成密码并连接非字母数字字符,直到达到所需的密码长度。

public string GeneratePassword(int length)
{
    var sb = new StringBuilder(length);

    while (sb.Length < length)
    {
        var tmp = System.Web.Security.Membership.GeneratePassword(length, 0);

        foreach(var c in tmp)
        {
            if(char.IsLetterOrDigit(c))
            {
                sb.Append(c);

                if (sb.Length == length)
                {
                    break;
                }
            }
        }
    }

    return sb.ToString();
}
nmpmafwu

nmpmafwu5#

Breigo的解决方案也有类似的方法,可能不是很有效,但很清晰和简短

string GeneratePassword(int length)
{
     var password = "";
     while (password.Length < length)
     {
          password += string.Concat(Membership.GeneratePassword(1, 0).Where(char.IsLetterOrDigit));
     }
     return password;
}
ldfqzlk8

ldfqzlk86#

我也更喜欢GUID方法-下面是简短的版本:

string password = Guid.NewGuid().ToString("N").Substring(0, 8);
jjhzyzn0

jjhzyzn07#

从@SollyM的答案开始,在它周围放一个while循环,以防止出现所有字符都是特殊字符或太多字符都是特殊字符的极不可能的事件,然后substring抛出异常。

private string GetAlphaNumericRandomString(int length)
{
    string randomString = "";
    while (randomString.Length < length)
    {
      //generates a random string, of twice the length specified, to counter the 
      //probability of the while loop having to run a second time
      randomString += Membership.GeneratePassword(length * 2, 0);

      //replace non alphanumeric characters
      randomString = Regex.Replace(randomString, @"[^a-zA-Z0-9]", m => "");
    }
    return randomString.Substring(0, length);
}
bnlyeluc

bnlyeluc8#

这就是我所使用的:

public class RandomGenerator
{
    //Guid.NewGuid().ToString().Replace("-", "");
    //System.Web.Security.Membership.GeneratePassword(12, 0);

    private static string AllowChars_Numeric = "0123456789";
    private static string AllowChars_EasyUpper = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static string AllowChars_EasyLower = "0123456789abcdefghijklmnopqrstuvwxyz";
    private static string AllowChars_Upper_Lower = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static string AllowedChars_Difficult = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@$^*()";

    public enum Difficulty
    {
        NUMERIC = 1,
        EASY_LOWER = 2,
        EASY_UPPER = 3,
        UPPER_LOWER = 4, 
        DIFFICULT = 5
    }

    public static string GetRandomString(int length, Difficulty difficulty)
    {
        Random rng = new Random();
        string charBox = AllowedChars_Difficult;

        switch (difficulty)
        {
            case Difficulty.NUMERIC:
                charBox = AllowChars_Numeric;
                break;
            case Difficulty.EASY_LOWER:
                charBox = AllowChars_EasyUpper;
                break;
            case Difficulty.EASY_UPPER:
                charBox = AllowChars_EasyLower;
                break;
            case Difficulty.UPPER_LOWER:
                charBox = AllowChars_Upper_Lower;
                break;
            case Difficulty.DIFFICULT:
            default:
                charBox = AllowedChars_Difficult;
                break;
        }

        char[] chars = new char[length];

        for (int i=0; i< length; i++)
        {
            chars[i] = charBox[rng.Next(0, charBox.Length)];
        }

        return new string(chars);
    }
}

相关问题