【问题标题】:Asp.net membership GeneratePassword method not creating password with only 2 special charactersAsp.net 成员 GeneratePassword 方法不创建只有 2 个特殊字符的密码
【发布时间】:2014-09-01 09:33:50
【问题描述】:

我的应用程序在 asp.net 4.0 中,我想生成 20 个字符的密码,其中只允许 2 个特殊字符。为了生成这个密码,我使用了下面的 if 代码。但这会创建包含 2 个以上特殊字符的密码。

   string randomPassword = Membership.GeneratePassword(20,2);

【问题讨论】:

    标签: asp.net-membership asp.net-4.0


    【解决方案1】:

    https://msdn.microsoft.com/en-us/library/system.web.security.membership.generatepassword%28v=vs.110%29.aspx

    numberOfNonAlphanumericCharacters 类型:System.Int32

    生成的密码中非字母数字字符(如@、#、!、%、& 等)的最少数量。

    我认为如果你想要正好有 2 个非字母数字字符,你应该创建自己的方法。

    public string CreatePassword(int numberChar, int numberSpecialChar){
    string password = "";
    string specialChars = "&@~#'[|\\^/!?;"; //choose you own
    int counterNonAlphanumeric = 0;
    for (int i=0; i <numberChar-numberSpecialChar; i++){
        password += generateRandomChar();
        if (specialChars.Contains(password.Substring(password.Length-1, 1))){
            counterNonAlphanumeric +=1;
        }
    }
    if (counterNonAlphanumeric != numberSpecialChar){
        password += generateRandomSpecialChar();
        password += generateRandomSpecialChar();
    }
    }
    

    我只是即兴创作了一些东西来展示这个想法。

    希望对你有帮助:P

    编辑:

    我发现了这个(已经创建的函数): Generating Random Passwords 有些功能正是您要搜索的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 2020-10-05
      • 2021-10-12
      • 2014-03-03
      相关资源
      最近更新 更多