Advertisement
Guest User

PasswordValidator

a guest
Nov 30th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. using System;
  2.  
  3. namespace AP_Question5_BManke
  4. {
  5.     class UserAccount
  6.     {
  7.         private string mUsername;
  8.         private string mPassword;
  9.  
  10.         public string Username
  11.         {
  12.             get { return mUsername; }
  13.             set
  14.             {
  15.                 if (ValidateUsername(value))
  16.                 {
  17.                     mUsername = value;
  18.                 }
  19.                 else
  20.                 {
  21.                     throw new Exception("The username must be 3-15 characters long, and contain only alphanumeric characters (letters A-Z, numbers 0-9) and the underscore character(_).");
  22.                 }
  23.             }
  24.         }
  25.  
  26.         public string Password
  27.         {
  28.             get { return mPassword; }
  29.             set
  30.             {
  31.                 if (ValidatePassword(value))
  32.                 {
  33.                     mPassword = value;
  34.                 }
  35.                 else
  36.                 {
  37.                     throw new Exception("The password must be 8-80 characters long with no spaces, contain at least one upper case and one lower case character, " +
  38.                         "contain a number ranging from 0-9, and have at least one special character (!@#$%^*_=+.?-)");
  39.                 }
  40.             }
  41.         }
  42.         public UserAccount()
  43.         {
  44.             mUsername = "user1";
  45.             mPassword = "P@ssword1";
  46.         }
  47.         public UserAccount(string username, string password)
  48.         {
  49.             Username = username;
  50.             Password = password;
  51.         }
  52.  
  53.         public bool HasSpecialChars(string username)
  54.         {
  55.             return username.Contains("_");
  56.         }
  57.  
  58.         static bool ValidatePassword(string password)
  59.         {
  60.             const int MIN_LENGTH = 8;
  61.             const int MAX_LENGTH = 80;
  62.  
  63.             if (password == null) throw new ArgumentNullException();
  64.  
  65.             bool meetsLengthRequirements = password.Length >= MIN_LENGTH && password.Length <= MAX_LENGTH;
  66.             bool hasUpperCaseLetter = false;
  67.             bool hasLowerCaseLetter = false;
  68.             bool hasDecimalDigit = false;
  69.  
  70.             if (meetsLengthRequirements)
  71.             {
  72.                 foreach (char c in password)
  73.                 {
  74.                     if (char.IsUpper(c)) hasUpperCaseLetter = true;
  75.                     else if (char.IsLower(c)) hasLowerCaseLetter = true;
  76.                     else if (char.IsDigit(c)) hasDecimalDigit = true;
  77.                 }
  78.             }
  79.  
  80.             bool isValid = meetsLengthRequirements
  81.                         && hasUpperCaseLetter
  82.                         && hasLowerCaseLetter
  83.                         && hasDecimalDigit
  84.                         ;
  85.             return isValid;
  86.  
  87.         }
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement