PasswordGenerator 包正在生成不需要的特殊字符

PasswordGenerator package is generating unwanted special char

我正在使用 PasswordGenerator(2.05) 包来生成密码。

其中一种方法允许我选择可以包括哪些特殊字符。

这是我的代码:

public static string GetUniquePassword(
        int i_lengthOfPassword = 8,
        bool i_includeLowercase = true, 
        bool i_includeUppercase = true, 
        bool i_includeNumeric = true,
        bool i_includeSpecial = true)
    {
        var pwd = new Password(includeLowercase: i_includeLowercase,
                               includeUppercase: i_includeUppercase,
                               includeNumeric: i_includeNumeric,
                               includeSpecial: i_includeSpecial,
                               passwordLength: i_lengthOfPassword).IncludeSpecial("!@#$%^&*,");

        string passwordResult = pwd.Next();
        return passwordResult;
    }

实际结果是,我偶尔会收到一个包含特殊字符 \ 的密码,而我的应用不允许这样做。

例如:2$Y!f0.

如果生成器不包含此字符,我会很高兴。

有什么解决方法吗?

如果我没看错 code,您实际上想要的是:

IPassword pwd = new Password(includeLowercase: i_includeLowercase,
                       includeUppercase: i_includeUppercase,
                       includeNumeric: i_includeNumeric,
                       includeSpecial: false,
                       passwordLength: i_lengthOfPassword);

if (i_includeSpecial)
{
    pwd = pwd.IncludeSpecial("!@#$%^&*,");
}

您需要在 Password 构造函数中将 includeSpecial 设置为 false,否则 constructor of PasswordSettings(它调用的)将已经添加了所有常用的特殊字符。

.NET Fiddle