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"));
}
}
public bool IsValidUsername(string newpassword)
{
using (dbHealthSplashEntities dbcontext = new dbHealthSplashEntities())
{
return !dbcontext.Admins.Any(user => user.Password == newpassword);
}
}
は、ここで私の見解モデル
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");
}
}
です//私は私のリポジトリのコードです。// MVCで同じにすることはできません私の古いパスワードが新しいパスワードと一致することを確認できません 私は古いパスワードからそれを検証したいのですが、それがエラーを投げるべきであるよりもパスワードと一致する場合は、これを助けてください
パスワードが既に存在するかどうかを確認できるということは、クリアテキストで保存されていることを意味します。あなたは本当に必要なのですか? –
解決に感謝しましたが、私はそれを解決しました –