Guest User

Untitled

a guest
Jul 17th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. public int Id {get;set;}
  2. public string Username { get; set; }
  3. public string Pwd { get; set; }
  4.  
  5. public class ConfirmPassword : IValidatableObject
  6. {
  7. [Required]
  8. public string Password { get; set; }
  9. [Required(ErrorMessage="Confirm Password field is required.")]
  10. public string ConfirmPwd { get; set; }
  11.  
  12. public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
  13. {
  14. string regex1 = @"^.{8,10}$"; // 8 - 10 characters
  15.  
  16. Match requirement1 = Regex.Match(Password, regex1);
  17.  
  18. if (Password != ConfirmPwd)
  19. yield return new ValidationResult("Password and Confirm Password is not identical.");
  20.  
  21. if (!requirement1.Success)
  22. yield return new ValidationResult("Password must be between 8 and 10 characters.");
  23.  
  24.  
  25. }
  26. }
  27.  
  28. namespace
  29. {
  30. public class User
  31. {
  32. public int Id {get;set;}
  33. public string Username { get; set; }
  34. public string Pwd { get; set; }
  35.  
  36. }
  37. }
  38.  
  39.  
  40. namespace
  41. {
  42. public class ConfirmPassword : IValidatableObject
  43. {
  44. [Required]
  45. public string Password { get; set; }
  46. [Required(ErrorMessage="Confirm Password field is required.")]
  47. public string ConfirmPwd { get; set; }
  48.  
  49. public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
  50. {
  51. string regex1 = @"^.{8,10}$"; // 8 - 10 characters
  52. string regex2 = @"(?:.*?[A-Z]){1}"; // 1 uppercase
  53. string regex3 = ""; // 1 lowercase
  54. string regex4 = ""; // 1 numeric
  55.  
  56. Match requirement1 = Regex.Match(Password, regex1);
  57. Match requirement2 = Regex.Match(Password, regex2);
  58. Match requirement3 = Regex.Match(Password, regex3);
  59. Match requirement4 = Regex.Match(Password, regex4);
  60.  
  61. if (Password != ConfirmPwd)
  62. yield return new ValidationResult("Password and Confirm Password is not identical.");
  63.  
  64. if (!requirement1.Success)
  65. yield return new ValidationResult("Password must be between 8 and 10 characters.");
  66.  
  67. if (!requirement2.Success)
  68. yield return new ValidationResult("Password must contain at least 1 uppercase letter.");
  69.  
  70. if (!requirement3.Success)
  71. yield return new ValidationResult("Password must contain at least 1 lowercase letter.");
  72.  
  73. if (!requirement4.Success)
  74. yield return new ValidationResult("Password must contain at least 1 numeric character.");
  75.  
  76. }
  77. }
  78. }
  79.  
  80. public LoginViewModel()
  81. {
  82. [Required]
  83. public string LoginId {get; set;}
  84.  
  85. [DataType(DataType.Password)]
  86. public string Password {get; set;}
  87.  
  88. [DataType(DataType.Password)]
  89. public string ConfirmPassword {get; set;}
  90.  
  91. // this validation is not db related and can be done client side...
  92. public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
  93. {
  94. string regex1 = @"^.{8,10}$"; // 8 - 10 characters
  95.  
  96. Match requirement1 = Regex.Match(Password, regex1);
  97.  
  98. if (Password != ConfirmPwd)
  99. yield return new ValidationResult("Password and Confirm Password is not identical.");
  100.  
  101. if (!requirement1.Success)
  102. yield return new ValidationResult("Password must be between 8 and 10 characters.");
  103.  
  104.  
  105. }
  106.  
  107. }
  108.  
  109. [HttpGet]
  110. public ActionResult Login()
  111. {
  112. var model = new LoginViewModel();
  113.  
  114. return View("Login",model);
  115. }
  116.  
  117. [HttpPost]
  118. public ActionResult Login(LoginViewModel model)
  119. {
  120.  
  121. var user = GetUserFromRepositoryByUsername(model.username);
  122.  
  123. if(user != null)
  124. {
  125. if(user.password == model.Password)
  126. {
  127. RedirectToAction("YouLoggedInYay!");
  128. }
  129. }
  130.  
  131. // if we made it this far, the user didn't exist or the password was wrong.
  132. // Highlight the username field red and add a validation error message.
  133. ModelState.AddError("Username","Your credentials were invalid punk!");
  134.  
  135. return View("Login",model);
  136. }
  137.  
  138. public class ConfirmPassword
  139. {
  140. User model;
  141.  
  142. [Required]
  143. public string Username
  144. {
  145. get { return this.model.Username; }
  146. set { this.model.Username = value; }
  147. }
  148. [Required]
  149. [DataType(DataType.Password)]
  150. public string Password
  151. {
  152. get { return this.model.Pwd; }
  153. set { this.model.Pwd = value; }
  154. }
  155.  
  156. [Required(ErrorMessage = "Confirm Password field is required.")]
  157. [Compare("NewPassword",
  158. ErrorMessage = "The new password and confirmation password do not match.")]
  159. [RegularExpression(@"^.{8,10}$")]
  160. [DataType(DataType.Password)]
  161. public string ConfirmPwd { get; set; }
  162.  
  163. public ConfirmPassword()
  164. {
  165. this.model = new User();
  166. }
  167.  
  168. public ConfirmPassword(User model)
  169. {
  170. this.model = model;
  171. }
  172.  
  173. }
Add Comment
Please, Sign In to add comment