查找字符串中的字符以检查密码复杂性c#

时间:2015-02-18 08:46:03

标签: c#

我试图像这样进行密码复杂性测试,但它无法正常工作。

 private void button1_Click(object sender, EventArgs e)
        {
            string CapsChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string LowerChars = "abcdefghijklmnopqrstuvwxyz";
            string Digits = "0123456789";
            string SpecialChars = "#<>!~@";

            if (textBox1.Text.ContainsInvariant(CapsChars))
            {
                if (textBox1.Text.ContainsInvariant(LowerChars))
                {
                    if (textBox1.Text.ContainsInvariant(Digits))
                    {
                        if (textBox1.Text.ContainsInvariant(SpecialChars))
                        {
                            MessageBox.Show("ALL Ok");
                        }
                        else
                        {
                            MessageBox.Show("No special character found");
                        }
                    }
                    else
                    {
                        MessageBox.Show("No digit found");
                    }
                }
                else
                {
                    MessageBox.Show("No lower case character found");
                }
            }
            else
            {
                MessageBox.Show("No upper case character found");
            }

        }

public static class StringExt
    {
        public static bool ContainsInvariant(this string sourceString, string filter)
        {
            return sourceString.ToLowerInvariant().Contains(filter);
        }
    }

我喜欢以这样一种方式测试:密码必须有一个大写字符和一个小写字符和一个数字和一个特殊字符和密码长度必须大于8.寻找建议如何实现它。只是想知道我们可以使用LINQ实现相同的目标吗?感谢

2 个答案:

答案 0 :(得分:3)

不确定这是最好的变体。比你的更容易:

using System;
using System.Linq;

namespace PasswordCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckPasswordComplexity("abc");
            CheckPasswordComplexity("aB1#");
            CheckPasswordComplexity("ABCc");
        }

        static private void CheckPasswordComplexity(string text)
        {
            var hasUpperCase = text.Any(char.IsUpper);
            var hasLowerCase = text.Any(char.IsLower);
            var hasDigits = text.Any(char.IsDigit);
            var hasPuncutation = text.Any(char.IsPunctuation); // Convers more than your case. Check here: http://www.dotnetperls.com/char-ispunctuation

            if (!hasUpperCase)
            {
                Console.WriteLine("No upper case character found");
            }
            else if (!hasLowerCase)
            {
                Console.WriteLine("No lower case character found");
            }
            else if (!hasDigits)
            {
                Console.WriteLine("No digit found");
            }
            else if (!hasPuncutation)
            {
                Console.WriteLine("No special character found");
            }
            else
            {
                Console.WriteLine("ALL Ok");
            }
        }
    }

答案 1 :(得分:1)

我想你需要重写ContainsInvariant方法

public static class StringExt
{
    public static bool ContainsInvariant(this string sourceString, string filter)
    {
        return sourceString.Any(c=>filter.Contains(c));
    }
}

上一个变体return sourceString.ToLowerInvariant().Contains(filter)忽略大写字母并搜索完整字符串匹配,而非单字母匹配

您需要在类代码之前添加using System.Linq;以使用方法Any()