I'm trying to make a website with asp.net mvc 4 and entity framework 6 where user can update their password in UserPassword view. Password is stored as Session. I'm storing the passwords in mssql database. Everything is working fine but whenever I click to change the password, it's not changing at all. I look at my table & I can see the previous password. Here are my codes,
Controller
[HttpPost]
public ActionResult UserPassword(UserInfo pass)
{
if (Session["UserNAME"] != null)
{
var PrevPass = testdb.UserInfoes.Where(a => a.Password.Equals(pass.Password)).FirstOrDefault();
if (Session["UserPASS"] == PrevPass)
{
if (ModelState.IsValid)
{
testdb.Entry(pass).State = System.Data.Entity.EntityState.Modified;
testdb.SaveChanges();
Session["UserPass"] = PrevPass.Password.ToString();
return RedirectToAction("UserLogin");
}
}
else
{
return RedirectToAction("ProfileView");
}
return View(pass);
}
else
{
return RedirectToAction("Login");
}
}
View
@using (Html.BeginForm("UserPassword", "Home", FormMethod.Post)) {
@Html.ValidationSummary(true)
<div class="editor-label">
<strong>Current Password</strong>
</div>
<div class="editor-field">
@Html.PasswordFor(a => a.Password)
@Html.ValidationMessageFor(a => a.Password)
</div>
<div class="editor-label">
<strong>New Password</strong>
</div>
<div class="editor-field">
@Html.PasswordFor(a => a.Password)
@Html.ValidationMessageFor(a => a.Password)
</div>
<div class="editor-label">
<strong>Retype Password</strong>
</div>
<div class="editor-field">
@Html.PasswordFor(a => a.Password)
@Html.ValidationMessageFor(a => a.Password)
</div><br />
<p><input type="submit" class="btn btn-info" value="Update" /></p>
}
Model
public partial class UserInfo
{
public string UserId { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public string AddressLine1 { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public System.DateTime CreatedDate { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string IsInfoMatched { get; set; }
public string IsApproved { get; set; }
public string IsActive { get; set; }
public string IsReportView { get; set; }
}
Is there something wrong in my code? If so, please I need a solution badly. I'm new to asp.net so I'm trying hard to master it. Your help will be a lifesaving for me. Tnx.
Password! Only the first one will bind when you post back. Whatever you think your entering under the headings ofNew PasswordandRetype Passwordare being ignored. You need to show the model forUserInfoOldPassword,NewPasswordandConfirmPasswordwith validation attributes including[Compare]. But stop right now. Your code suggests your not even hashing your passwords in the database. Create a new project with forms authentication in VS and study the code in theAccountControllerand its associated view models to see how you should be handling all this.