i want to change the current password using asp.net mvc using entity framework.
Using Asp.Net Identity, you can reset password(change password ) with the following C# code in your controller
// Get View using this Method
public ActionResult ChangePassword()
{
return View();
}
//
//Save Details of New Password using Identity
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId<int>(), model.OldPassword, model.NewPassword);
if (result.Succeeded)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
if (user != null)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
}
return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
}
AddErrors(result);
return View(model);
}
and in the View, your code can be
@model YourProject.Models.ChangePasswordViewModel
@{
ViewBag.Title = "Change Password";
}
<h1>@ViewBag.Title</h1>
@using (Html.BeginForm("ChangePassword", "ControllerName", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.OldPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.OldPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.NewPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Change password" class="btn btn-primary" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
ChangePasswordViewModel.cs code looks like this
public class ChangePasswordViewModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
The above code is what you will get if you will create asp.net MVC project with the Basic template instead of "Empty" template, as this template auto-generate these code.
If you are not using ASP.NET identity, you can add it in your Project or try looking at forgot password functionality in mvc article
You can use it with Entity Framework.
Above answer should work for you, but as your details of the question is not enough & it may not work, then take a look at these url's for help
https://stackoverflow.com/questions/33988925/mvc-net-password-change
Reset Password : http://foxlearn.com/article/implement-password-reset-with-asp-net-identity-mvc-37.html
Hope this helps, if not please provide better details on your questions
Without Identity to change password, you can follow this procedure
Create a Model to change the password
public class UserModel{
public string Email{get;set;}
public string Password{get;set;}
public string NewPassword{get;set;}
}
In the View, Create a form with above Model as reference
@using (Html.BeginForm("Changepassword", "Home", FormMethod.Post))
{
<table class="center">
<tr>
<td>Email</td>
<td>
@Html.EditorFor(pass => pass.Email)
</td>
<td>@Html.ValidationMessageFor(pass => pass.Email)</td>
</tr>
<tr>
<td>Old Password</td>
<td>
@Html.EditorFor(pass => pass.Password)
</td>
<td>@Html.ValidationMessageFor(pass => pass.Password)</td>
</tr>
<tr class="rowspace">
<td>New Password</td>
<td>
@Html.EditorFor(pass => pass.NewPassword)
</td>
<td>@Html.ValidationMessageFor(pass => pass.NewPassword)</td>
</tr>
<tr class="rowspace">
<td colspan="3" id="button">
<input type="submit" value="Update Password" /></td>
</tr>
<tr class="rowspace"><td colspan="3">@ViewBag.Message</td></tr>
</table>
}
Now in the C# controller, get form, check if old password matches with what user has entered and then save new password
public ActionResult Changepassword(UserModel login)
{
using (UserDetailsEntities db = new UserDetailsEntities())
{
var detail = db.tblUsers.Where(log => log.Password == login.Password
&& log.email == login.email).FirstOrDefault();
if (detail != null)
{
userDetail.Password = login.NewPassword;
db.SaveChanges();
ViewBag.Message = "Password updated Successfully!";
}
else
{
ViewBag.Message = "Password not Updated!";
}
}
return View(login);
}
That's it, thanks.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly