You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.6 KiB

  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace Common.Utility
  4. {
  5. public class CheckPasswordUtility
  6. {
  7. protected string dob = "";
  8. protected string idNumber = "";
  9. protected string email = "";
  10. protected string mobile = "";
  11. protected string idType = "";
  12. public CheckPasswordUtility()
  13. {
  14. //dob = EncryptDecryptUtility.ReadSession("birthDate", "");
  15. //idNumber = EncryptDecryptUtility.ReadSession("senderIdNo", "").Replace("-", "");
  16. //email = EncryptDecryptUtility.ReadSession("username", "");
  17. //mobile = EncryptDecryptUtility.ReadSession("mobile", "");
  18. //idType = EncryptDecryptUtility.ReadSession("senderIdType", "");
  19. }
  20. private void SetParams(string dobR, string idNumberR, string emailR, string mobileR, string idTypeR)
  21. {
  22. dob = string.IsNullOrEmpty(dobR) ? dob : dobR;
  23. idNumber = string.IsNullOrEmpty(idNumberR) ? idNumber : idNumberR;
  24. email = string.IsNullOrEmpty(emailR) ? email : emailR;
  25. mobile = string.IsNullOrEmpty(mobileR) ? mobile : mobileR;
  26. idType = string.IsNullOrEmpty(idTypeR) ? idType : idTypeR;
  27. }
  28. public string CheckPassword(string password, string dobR = "", string idNumberR = "", string emailR = "", string mobileR = "", string idTypeR = "")
  29. {
  30. SetParams(dobR, idNumberR, emailR, mobileR, idTypeR);
  31. string errMsg = "";
  32. errMsg = CheckForRegExp(password);
  33. if (!string.IsNullOrEmpty(errMsg))
  34. {
  35. return errMsg;
  36. }
  37. errMsg = CheckForEmail(password);
  38. if (!string.IsNullOrEmpty(errMsg))
  39. {
  40. return errMsg;
  41. }
  42. //errMsg = CheckMobile(password);
  43. //if (!string.IsNullOrEmpty(errMsg))
  44. //{
  45. // return errMsg;
  46. //}
  47. //errMsg = CheckIdNumber(password);
  48. //if (!string.IsNullOrEmpty(errMsg))
  49. //{
  50. // return errMsg;
  51. //}
  52. //errMsg = CheckDob(password);
  53. return errMsg;
  54. }
  55. private string CheckForRegExp(string password)
  56. {
  57. string patternPassword = @"^(?=.*\d)(?=.*[A-Z]).{9,30}$";
  58. if (!string.IsNullOrEmpty(password))
  59. {
  60. if (!Regex.IsMatch(password, patternPassword))
  61. {
  62. return "Password must meet the following requirements: At least one symbol / At least one capital letter / At least one number / Be at least 9 characters";
  63. }
  64. }
  65. Regex r = new Regex(@"[~`!@#$%^&*()-+=|\{}':;.,<>/?]");
  66. if (!r.IsMatch(password))
  67. {
  68. return "Password must meet the following requirements: At least one symbol / At least one capital letter / At least one number / Be at least 9 characters";
  69. }
  70. return "";
  71. }
  72. private string CheckDob(string password)
  73. {
  74. string[] dobArr = null;
  75. string dateOfBirth = "";
  76. if (idType.ToLower() == "passport" || idType.Trim().Equals("10997"))
  77. {
  78. dobArr = dob.Split('/');
  79. string mm = dobArr[0], dd = dobArr[1];
  80. if (dobArr[0].Length == 1)
  81. {
  82. mm = "0" + mm;
  83. }
  84. if (dobArr[1].Length == 1)
  85. {
  86. dd = "0" + dd;
  87. }
  88. dateOfBirth = dobArr[2].Substring(0, 4) + mm + dd;
  89. }
  90. else
  91. {
  92. dateOfBirth = idNumber.Substring(0, 6);
  93. string yy = "19" + dateOfBirth.Substring(0, 2);
  94. int nowYear = DateTime.Now.Year;
  95. if ((nowYear - Convert.ToInt16(yy)) > 80)
  96. {
  97. yy = "20" + dateOfBirth.Substring(0, 2);
  98. }
  99. dateOfBirth = yy + dateOfBirth.Substring(2, 4);
  100. }
  101. string dateOfBirth1 = dateOfBirth.Substring(0, 4);
  102. string dateOfBirth2 = dateOfBirth.Substring(2, 6);
  103. string dateOfBirth3 = dateOfBirth.Substring(4, 4);
  104. if (password.Contains(dateOfBirth1))
  105. {
  106. return "Password can not be same as DOB!";
  107. }
  108. if (password.Contains(dateOfBirth2))
  109. {
  110. return "Password can not be same as DOB!";
  111. }
  112. if (password.Contains(dateOfBirth3))
  113. {
  114. return "Password can not be same as DOB!";
  115. }
  116. return "";
  117. }
  118. private string CheckIdNumber(string password)
  119. {
  120. if (idType.ToLower() == "passport")
  121. {
  122. if (password.Contains(idNumber))
  123. {
  124. return "Password can not be same as id Number!";
  125. }
  126. }
  127. string idFirstPart = idNumber.Substring(0, 6);
  128. string idSecondPart = idNumber.Substring(6, idNumber.Length - 6);
  129. if (password.Contains(idFirstPart))
  130. {
  131. return "Password can not be same as id Number!";
  132. }
  133. if (password.Contains(idSecondPart))
  134. {
  135. return "Password can not be same as id Number!";
  136. }
  137. return "";
  138. }
  139. private string CheckMobile(string password)
  140. {
  141. string mobileNum = "";
  142. if (mobile.Contains("+82"))
  143. {
  144. mobileNum = mobile.Replace("+82", "0");
  145. }
  146. else
  147. {
  148. string mobileFirst2 = mobile.Substring(0, 2);
  149. if (mobileFirst2 == "82")
  150. {
  151. mobileNum = "0" + mobile.Substring(2, mobile.Length - 2);
  152. }
  153. else if (mobile.Substring(0, 1) != "0" && mobile.Length == 10)
  154. {
  155. mobileNum = "0" + mobileNum;
  156. }
  157. else
  158. {
  159. mobileNum = mobile;
  160. }
  161. }
  162. string mobileNum1 = mobileNum.Substring(3, mobileNum.Length - 3);
  163. if (password.Contains(mobileNum1))
  164. {
  165. return "Password can not be same as mobile number!";
  166. }
  167. return "";
  168. }
  169. private string CheckForEmail(string password)
  170. {
  171. var emailArr = email.Split('@');
  172. if (password.ToLower().Contains(emailArr[0].ToLower()))
  173. {
  174. return "Password can not be same as email!";
  175. }
  176. return "";
  177. }
  178. }
  179. }