私は 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")
}
誰かが私のコードでエラーを見つけるのを手伝ってくれますか? よろしくお願いします。RAM