密码未在MVC 4中的webpages_Membership中更新

时间:2013-10-30 20:16:08

标签: asp.net-mvc-4 passwords reset

我是MVC 4的新手,我正在尝试设置一个自助服务工具,用于在用户忘记密码时重置密码,使用在注册时添加到UserProfile表中的3个问题的答案,而不是诉诸发送电子邮件。应用程序运行时没有给我一个错误。但密码未在webpages_Membership中更新。

所有相关代码如下。

   Models:
   AccountModels.cs
public class ForgotPasswordModel
    {
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }
    [Required]
    [Display(Name = "What is your place of birth?")]
    public string PlaceOfBirth { get; set; }

    [Required]
    [Display(Name = "What is your Mother's maiden name?")]
    public string MotherName { get; set; }

    [Required]
    [Display(Name = "What is the name of your first School?")]
    public string SchoolName { get; set; }
    }

public class ResetPasswordModel
    {
    [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; }
    }


    [Table("UserProfile")]
    public class UserProfile
    {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string FullName { get; set; }
    public string EmailId { get; set; }
    public string PlaceOfBirth { get; set; }
    public string MotherName { get; set; }
    public string SchoolName { get; set; }
    }

 
    [Table("webpages_Membership")]
    public class webpages_Membership 
    {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]      
    public int UserId {get; set; }  
    public DateTime CreateDate {get; set; }
    public string ConfirmationToken {get; set; }                      
    public int IsConfirmed {get; set; }
    public DateTime LastPasswordFailureDate { get; set; }             
    public int PasswordFailuresSinceLastSuccess  {get; set; }
    public string Password {get; set; }
    public DateTime PasswordChangedDate { get; set; }                   
    public string PasswordSalt {get; set; }                           
    public string PasswordVerificationToken {get; set; }
    public DateTime PasswordVerificationTokenExpirationDate { get; set; }
    }    

  CONTROLLER METHODS:
  AccountController.cs

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ForgotPassword(ForgotPasswordModel model)
    {
        UsersContext userContext = new UsersContext();
        var username = model.UserName;        
        // Get UserProfile from current user name in the form
        var dt=userContext.UserProfiles.FirstOrDefault(d=>d.UserName.Equals(username));
        var dbPlaceOfBirth = dt.PlaceOfBirth;
        var dbMotherName = dt.MotherName;
        var dbSchoolName = dt.SchoolName;

        if (model.PlaceOfBirth == dbPlaceOfBirth && model.MotherName == dbMotherName &&   model.SchoolName == dbSchoolName)
        return RedirectToAction("ResetPassword", "Account", new { username = username });
        else
        {
        TempData["Message"] =  "The user name provided is incorrect.";
        return View(model);
        }
    }




    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult ResetPassword(ResetPasswordModel model, string username)
    {
        UsersContext db = new UsersContext();
        var token = WebSecurity.GeneratePasswordResetToken(username);
        var userid = (from i in db.UserProfiles
                     where i.UserName == username
                      select i.UserId).FirstOrDefault();
        var dt = db.webpages_Memberships.FirstOrDefault(d => d.UserId.Equals(userid));
        dt.Password = model.NewPassword;

            bool response = WebSecurity.ResetPassword(token, dt.Password);
            if (response == true)
            {
                return RedirectToAction("~/account/login");
            }
            else
            {
                TempData["Message"] = "An eror has occurred";
                return View(model);
            }

    }

视图

ForgotPassword.cshtlml

@model Oyster.Models.ForgotPasswordModel
@{
    ViewBag.Title = "Create a new Password";
}

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
    <h4> All fields must be entered.</h4>
        <div class="password-label">
            @Html.LabelFor(m => m.UserName)
        </div>
        <div class="password-field">
            @Html.TextBoxFor(m => m.UserName)
        </div>
        <div class="password-label">
            @Html.LabelFor(m => m.PlaceOfBirth)
        </div>
        <div class="password-field">
            @Html.PasswordFor(m => m.PlaceOfBirth)
        </div>
        <div class="password-label">
            @Html.LabelFor(m => m.MotherName)
        </div>
        <div class="password-field">
            @Html.PasswordFor(m => m.MotherName)
        </div>
        <div class="password-label">
            @Html.LabelFor(m => m.SchoolName)
        </div>
        <div class="password-field">
            @Html.PasswordFor(m => m.SchoolName)
        </div>
        <input type="submit" value="Submit" onclick="this.disabled=true"/>
         @Html.ActionLink("Cancel", "Login") 
    </fieldset>
}

ResetPassword.cshtml

@model Oyster.Models.ResetPasswordModel
@{
    ViewBag.Title = "Reset Password Form";
}

    @using (Html.BeginForm("ResetPassword", "Account"))
    {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>Reset Password Form</legend>
        <div class="login-label">
            @Html.LabelFor(m => m.NewPassword)
            @Html.PasswordFor(m => m.NewPassword)
        </div>
        <div class="login-label">
            @Html.LabelFor(m => m.ConfirmPassword)
            @Html.PasswordFor(m => m.ConfirmPassword)
        </div>
    </fieldset>
        <input type="submit" value="Reset password" onclick="this.disabled=true"/>
        @Html.ActionLink("Cancel", "Login")

}

有人可以帮我找一下代码中的错误吗? 首先十分感谢。 拉姆

0 个答案:

没有答案