PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/MyVMPortal/ValidationHelper.cs

https://github.com/mwasham/wasvcmgmntapi
C# | 101 lines | 79 code | 19 blank | 3 comment | 11 complexity | add5d22e47ce97c9c51334804f00210b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace MyVMPortal
  8. {
  9. public class ValidationHelpers
  10. {
  11. private static readonly char[] WindowsComputerNameInvalidChars = @"`~!@#$%^&*()=+_[]{}\|;:.'"",<>/?".ToCharArray();
  12. private static readonly Regex NumericRegex = new Regex(@"^\d+$", RegexOptions.Singleline | RegexOptions.Compiled);
  13. private static readonly Regex PasswordHasLowerChar = new Regex(@"[a-z]", RegexOptions.Singleline | RegexOptions.CultureInvariant);
  14. private static readonly Regex PasswordHasUpperChar = new Regex(@"[A-Z]", RegexOptions.Singleline | RegexOptions.CultureInvariant);
  15. private static readonly Regex PasswordHasDigitChar = new Regex(@"\d", RegexOptions.Singleline | RegexOptions.CultureInvariant);
  16. private static readonly Regex PasswordHasSpecialChar = new Regex(@"\W", RegexOptions.Singleline | RegexOptions.CultureInvariant);
  17. private static readonly Regex[] PasswordCriteria = new Regex[] { PasswordHasLowerChar, PasswordHasUpperChar, PasswordHasDigitChar, PasswordHasSpecialChar };
  18. private const int PasswordMinComplexity = 3;
  19. private const int PasswordMinLength = 8;
  20. private const int PasswordMaxLength = 123;
  21. private const int LinuxUserNameMinLength = 1;
  22. private const int LinuxUserNameMaxLength = 64;
  23. private const int LinuxPasswordMinLength = 6;
  24. private const int LinuxPasswordMaxLength = 72;
  25. private const int WindowsComputerNameMaxLength = 15;
  26. public static bool IsLinuxPasswordValid(string password)
  27. {
  28. if (password.Length < LinuxPasswordMinLength || password.Length > LinuxPasswordMaxLength)
  29. {
  30. return false;
  31. }
  32. // Check complexity
  33. int complexity = PasswordCriteria.Count(criteria => criteria.IsMatch(password));
  34. if (complexity < PasswordMinComplexity)
  35. {
  36. return false;
  37. }
  38. return true;
  39. }
  40. public static bool IsWindowsPasswordValid(string password)
  41. {
  42. // Check length
  43. if (password.Length < PasswordMinLength || password.Length > PasswordMaxLength)
  44. {
  45. return false;
  46. }
  47. // Check complexity
  48. int complexity = PasswordCriteria.Count(criteria => criteria.IsMatch(password));
  49. if (complexity < PasswordMinComplexity)
  50. {
  51. return false;
  52. }
  53. return true;
  54. }
  55. public static bool IsLinuxHostNameValid(string hostName)
  56. {
  57. if (string.IsNullOrEmpty(hostName))
  58. {
  59. return false;
  60. }
  61. if (hostName.Length > 64)
  62. {
  63. return false;
  64. }
  65. return true;
  66. }
  67. public static bool IsWindowsComputerNameValid(string computerName)
  68. {
  69. if (string.IsNullOrEmpty(computerName))
  70. {
  71. return false;
  72. }
  73. if (computerName.Length > WindowsComputerNameMaxLength ||
  74. computerName.IndexOfAny(WindowsComputerNameInvalidChars) != -1 ||
  75. NumericRegex.IsMatch(computerName))
  76. {
  77. return false;
  78. }
  79. return true;
  80. }
  81. }
  82. }