2017-04-17 1 views
1
//Here Is my code below I am using Remote validation 

    public ActionResult Settings() 
     { 
      return View(); 
     } 

     //For checking New Password Doesnt match With Old Password 
     public JsonResult IsValidUsername(string newPassword) 
     { 
      AdminRepository service = new AdminRepository(); 
      return Json(service.IsValidUsername(newPassword), JsonRequestBehavior.AllowGet); 
     } 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Settings(AdminViewModels model, FormCollection collection) 
     { 
      try 
      { 
       ModelState.Remove("EmailID"); 
       ModelState.Remove("IsValid"); 


       if (ModelState.IsValid) 
       { 
        AdminViewModels adminObj = (AdminViewModels)Session["Admin"]; 
        model.AdminID = adminObj.AdminID; 
        string newPassword = collection["NewPassword"]; 
        if (!string.IsNullOrEmpty(newPassword)) 
        { 
         bool exists = admin.PasswordUpdate(model, newPassword); 
         if (exists) 
          ViewBag.message = "Password Updated Successfully"; 
         else 
          ViewBag.messageinvalid = "Invalid Password"; 
        } 
        else 
        { 
         ViewBag.nullmessage = "Enter New Password"; 
        } 
       } 

       return View(); 
      } 
      catch (Exception ex) 
      { 
       return View("Error", new HandleErrorInfo(ex, "model", "collection")); 
      } 

     } 

// Hier ist der Code von meinem RepositoryAlt und das neue Passwort nicht gleich sein in mvc

public bool IsValidUsername(string newpassword) 
     { 
      using (dbHealthSplashEntities dbcontext = new dbHealthSplashEntities()) 
      { 
       return !dbcontext.Admins.Any(user => user.Password == newpassword); 
      } 
     } 

hier // Modell meiner Sicht

public class AdminViewModels:IValidatableObject 
    { 
     public int AdminID { get; set; } 

     [Required(ErrorMessage = "EmailID is required")] 
     [StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)] 
     [RegularExpression("^[a-zA-Z0-9_\\.-][email protected]([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")] 
     [EmailAnnotation] 
     public string EmailID { get; set; } 

     [Required(ErrorMessage = "Password is required")] 
     //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
     [DataType(DataType.Password)] 
     [Display(Name = "Password")] 
     public string Password { get; set; } 


     [Required(ErrorMessage = "Enter New Password")] 
     //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
     [DataType(DataType.Password)] 
     [Display(Name = "newPassword")] 
     [Remote("IsValidUsername", "Admin", ErrorMessage = "It Seems You Have Entered Same Password As Old Password!!!")] 
     public string newPassword { get; set; } 

     [Required] 
     [System.ComponentModel.DataAnnotations.Compare("newPassword", ErrorMessage = "The password and confirmation password do not match.")] 
     public string ConfirmPassword { get; set; } 
     public Nullable<bool> Flag { get; set; } 

     public bool RememberMe { get; set; } 

     public bool IsValid { get; set; } 


     public IEnumerable<ValidationResult> Validate(ValidationContext context) 
     { 
      if (newPassword == Password) 
       yield return new ValidationResult("Passwords should not be the same"); 
     } 
    } 

ist // Ich bin kann nicht überprüfen, dass mein altes Passwort mit dem neuen Passwort übereinstimmt Ich möchte es vom alten Passwort bestätigen, wenn es mit dem Passwort übereinstimmt, als es einen Fehler werfen sollte, bitte helfen Sie mir aus diesem

+0

Die Tatsache, dass Sie überprüfen können, ob ein Passwort bereits existiert, bedeutet, dass es im Klartext gespeichert ist. Bist du * wirklich * sicher, dass das nötig ist? –

+0

danke für die Lösung, aber ich hatte das gelöst –

Antwort

0
[HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Settings(AdminViewModels model, FormCollection collection) 
     { 
      try 
      { 
       ModelState.Remove("EmailID"); 
       ModelState.Remove("IsValid"); 


       if (ModelState.IsValid) 
       { 
        AdminViewModels adminObj = (AdminViewModels)Session["Admin"]; 
        model.AdminID = adminObj.AdminID; 
        string newPassword = collection["NewPassword"]; 
        if(model.Password== newPassword) 
        { 
         viewbag.errormessage="Old Password and new password cannot be same"; 
        } 
        else 
        { 

        if (!string.IsNullOrEmpty(newPassword)) 
        { 
         bool exists = admin.PasswordUpdate(model, newPassword); 
         if (exists) 
          ViewBag.message = "Password Updated Successfully"; 
         else 
          ViewBag.messageinvalid = "Invalid Password"; 
        } 
        else 
        { 
         ViewBag.nullmessage = "Enter New Password"; 
        } 
       } 


       return View(); 
} 
return View(); 
      } 
      catch (Exception ex) 
      { 
       return View("Error", new HandleErrorInfo(ex, "model", "collection"));enter code here 
      } 

     }`enter code here` 
Verwandte Themen