@{
Layout = "~/Shared/Layouts/_Layout.cshtml";
Page.Title = "Change Password";
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("~/Default");
}
var currentPassword = "";
var newPassword1 = "";
var newPassword2 = "";
if(IsPost)
{
currentPassword = Request["currentPassword"];
newPassword1 = Request["newPassword1"];
newPassword2 = Request["newPassword2"];
// Validation
if (currentPassword.IsEmpty()) {
ModelState.AddError("currentPassword", "Current Password required.");
}
if (newPassword1.IsEmpty()) {
ModelState.AddError("newPassword1", "Required.");
}
if (newPassword2.IsEmpty()) {
ModelState.AddError("newPassword2", "Required.");
}
if(newPassword1 != newPassword2)
{
ModelState.AddError("newPassword1", "The passwords do not match.");
}
// Attempt password change
if(ModelState.IsValid)
{
var currentUser = WebSecurity.CurrentUserName;
if(WebSecurity.ChangePassword(currentUser, currentPassword, newPassword1))
{
Response.Redirect("~/Default");
}
else
{
ModelState.AddFormError("Unable to change password.");
}
}
}
}

<h2>Change your password</h2> 
 
@Html.ValidationSummary(true)

<div class="form">
    <form action="ChangePassword" method="post" class="accountForm">
        <div>
    <p>
    <span class="formtitle">@Html.Label("Current Password: ", "currentPassword")<br /> </span>
    @Html.Password("currentPassword", currentPassword)
    @Html.ValidationMessage("currentPassword")
    </p>
        </div>
        <div>
    <p>
    <span class="formtitle">@Html.Label("New Password: ", "newPassword1")<br /></span>
    @Html.Password("newPassword1", newPassword1)
    @Html.ValidationMessage("newPassword1")
    </p>
        </div>
        <div>
    <p>
    <span class="formtitle">@Html.Label("Confirm New Password: ", "newPassword2")<br /></span>
    @Html.Password("newPassword2", newPassword2)
    @Html.ValidationMessage("newPassword2")
    </p>
        </div>
<p>
<input type="submit" value="Change Password" />
</p>
</form>
</div>