What exactly is classified as a “symbol” in C#?

2019-05-31 11:04发布

问题:

So I am trying to write a program that asks for you to create a password. I have a block of code that checks to see if the string entered by the user contains a symbol. I have the code set to exit the loop when the boolean value 'validPassword' equals true.

    string pleaseenterapassword = "Create a password:";
    bool validPassword = false;
    Console.WriteLine(pleaseenterapassword); // Writes to the screen "Create a password:"
    string password = Console.ReadLine(); //Sets the text entered in the Console into the string 'password'
    bool containsAtLeastOneSymbol = password.Any(char.IsSymbol);
    if (containsAtLeastOneSymbol == false) // Checks if your password contains at least one symbol
        {
            Console.WriteLine("Your password must contain at least one symbol.");
            validPassword = false;
        }

This code is successful if I enter something like "Thisismypassword905+", but it does not work if I enter something like "Thisismypassword95*". I would appreciate any sort of help. Thanks in advance!

回答1:

From msdn:

Valid symbols are members of the following categories in UnicodeCategory: MathSymbol, CurrencySymbol, ModifierSymbol, and OtherSymbol. Symbols in the Unicode standard are a loosely defined set of characters that include the following:

  • Currency symbols.
  • Letterlike symbols, which include a set of mathematical alphanumeric symbols as well as symbols such as ℅, №, and ™.
  • Number forms, such as subscripts and superscripts.
  • Mathematical operators and arrows
  • Geometric symbols.
  • Technical symbols.
  • Braille patterns.

  • Dingbats

Update: For juharr, who asked why does the '*' is not in the category of MathSymbol. The asterisk is not in the category of MathSymbols with this snippet you can check it. Or see this fiddle

      int ctr = 0;
      UnicodeCategory category = UnicodeCategory.MathSymbol;

      for (ushort codePoint = 0; codePoint < ushort.MaxValue; codePoint++) {
         Char ch = Convert.ToChar(codePoint);

         if (CharUnicodeInfo.GetUnicodeCategory(ch) == category) {
            if (ctr % 5 == 0)
               Console.WriteLine();
            Console.Write("{0} (U+{1:X4})     ", ch, codePoint);
            ctr++;
         } 
      }
      Console.WriteLine();
      Console.WriteLine("\n{0} characters are in the {1:G} category", 
                        ctr, category);   


回答2:

Back to your problem. I'd do it this way:

You could use PasswordValidator from Asp.Net Identity Framework but if you don't want to introduce such dependency is easy to extract the behavior using a couple of classes:

The validator

public class BasicPasswordPolicyValidator
    {
        /// <summary>
        ///     Minimum required length
        /// </summary>
        public int RequiredLength { get; set; }

        /// <summary>
        ///     Require a non letter or digit character
        /// </summary>
        public bool RequireNonLetterOrDigit { get; set; }

        /// <summary>
        ///     Require a lower case letter ('a' - 'z')
        /// </summary>
        public bool RequireLowercase { get; set; }

        /// <summary>
        ///     Require an upper case letter ('A' - 'Z')
        /// </summary>
        public bool RequireUppercase { get; set; }

        /// <summary>
        ///     Require a digit ('0' - '9')
        /// </summary>
        public bool RequireDigit { get; set; }

        public virtual ValidationResult Validate(string item)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            var errors = new List<string>();
            if (string.IsNullOrWhiteSpace(item) || item.Length < RequiredLength)
                errors.Add("Password is too short");
            if (RequireNonLetterOrDigit && item.All(IsLetterOrDigit))
                errors.Add("Password requires non letter or digit");
            if (RequireDigit && item.All(c => !IsDigit(c)))
                errors.Add("Password requires digit");
            if (RequireLowercase && item.All(c => !IsLower(c)))
                errors.Add("Password requires lowercase");
            if (RequireUppercase && item.All(c => !IsUpper(c)))
                errors.Add("Password requires uppercase");

            if (errors.Count == 0)
                return new ValidationResult
                {
                    Success = true,
                };

            return new ValidationResult
            {
                Success = false,
                Errors = errors
            };
        }

        /// <summary>
        ///     Returns true if the character is a digit between '0' and '9'
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual bool IsDigit(char c)
        {
            return c >= '0' && c <= '9';
        }

        /// <summary>
        ///     Returns true if the character is between 'a' and 'z'
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual bool IsLower(char c)
        {
            return c >= 'a' && c <= 'z';
        }

        /// <summary>
        ///     Returns true if the character is between 'A' and 'Z'
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual bool IsUpper(char c)
        {
            return c >= 'A' && c <= 'Z';
        }

        /// <summary>
        ///     Returns true if the character is upper, lower, or a digit
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual bool IsLetterOrDigit(char c)
        {
            return IsUpper(c) || IsLower(c) || IsDigit(c);
        }
    }

Validation Result

public class ValidationResult
    {
        public bool Success { get; set; }
        public List<string> Errors { get; set; }
    }

Your main function:

var pleaseenterapassword = "Create a password:";
            bool validPassword;

            //Initialize the password validator according to your needs
            var validator = new BasicPasswordPolicyValidator
            {
                RequiredLength = 8,
                RequireNonLetterOrDigit = true,
                RequireDigit = false,
                RequireLowercase = false,
                RequireUppercase = false
            };

            do
            {
                Console.WriteLine(pleaseenterapassword); // Writes to the screen "Create a password:"
                var password = Console.ReadLine(); //Sets the text entered in the Console into the string 'password'

                var validationResult = validator.Validate(password);
                validPassword = validationResult.Success;
                Console.WriteLine("Your password does not comply with the policy...");
                foreach (var error in validationResult.Errors)
                    Console.WriteLine("\tError: {0}", error);

            } while (!validPassword);

Hope this helps!