Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. //Here Is my code below I am using Remote validation
  2.  
  3. public ActionResult Settings()
  4. {
  5. return View();
  6. }
  7.  
  8. //For checking New Password Doesnt match With Old Password
  9. public JsonResult IsValidUsername(string newPassword)
  10. {
  11. AdminRepository service = new AdminRepository();
  12. return Json(service.IsValidUsername(newPassword), JsonRequestBehavior.AllowGet);
  13. }
  14.  
  15. [HttpPost]
  16. [ValidateAntiForgeryToken]
  17. public ActionResult Settings(AdminViewModels model, FormCollection collection)
  18. {
  19. try
  20. {
  21. ModelState.Remove("EmailID");
  22. ModelState.Remove("IsValid");
  23.  
  24.  
  25. if (ModelState.IsValid)
  26. {
  27. AdminViewModels adminObj = (AdminViewModels)Session["Admin"];
  28. model.AdminID = adminObj.AdminID;
  29. string newPassword = collection["NewPassword"];
  30. if (!string.IsNullOrEmpty(newPassword))
  31. {
  32. bool exists = admin.PasswordUpdate(model, newPassword);
  33. if (exists)
  34. ViewBag.message = "Password Updated Successfully";
  35. else
  36. ViewBag.messageinvalid = "Invalid Password";
  37. }
  38. else
  39. {
  40. ViewBag.nullmessage = "Enter New Password";
  41. }
  42. }
  43.  
  44. return View();
  45. }
  46. catch (Exception ex)
  47. {
  48. return View("Error", new HandleErrorInfo(ex, "model", "collection"));
  49. }
  50.  
  51. }
  52.  
  53. public bool IsValidUsername(string newpassword)
  54. {
  55. using (dbHealthSplashEntities dbcontext = new dbHealthSplashEntities())
  56. {
  57. return !dbcontext.Admins.Any(user => user.Password == newpassword);
  58. }
  59. }
  60.  
  61. public class AdminViewModels:IValidatableObject
  62. {
  63. public int AdminID { get; set; }
  64.  
  65. [Required(ErrorMessage = "EmailID is required")]
  66. [StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)]
  67. [RegularExpression("^[a-zA-Z0-9_\.-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]
  68. [EmailAnnotation]
  69. public string EmailID { get; set; }
  70.  
  71. [Required(ErrorMessage = "Password is required")]
  72. //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
  73. [DataType(DataType.Password)]
  74. [Display(Name = "Password")]
  75. public string Password { get; set; }
  76.  
  77.  
  78. [Required(ErrorMessage = "Enter New Password")]
  79. //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
  80. [DataType(DataType.Password)]
  81. [Display(Name = "newPassword")]
  82. [Remote("IsValidUsername", "Admin", ErrorMessage = "It Seems You Have Entered Same Password As Old Password!!!")]
  83. public string newPassword { get; set; }
  84.  
  85. [Required]
  86. [System.ComponentModel.DataAnnotations.Compare("newPassword", ErrorMessage = "The password and confirmation password do not match.")]
  87. public string ConfirmPassword { get; set; }
  88. public Nullable<bool> Flag { get; set; }
  89.  
  90. public bool RememberMe { get; set; }
  91.  
  92. public bool IsValid { get; set; }
  93.  
  94.  
  95. public IEnumerable<ValidationResult> Validate(ValidationContext context)
  96. {
  97. if (newPassword == Password)
  98. yield return new ValidationResult("Passwords should not be the same");
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement