-1

我正在 ASP.NET MVC4 中实现忘记密码功能,但无法成功允许用户重置密码。

我的 MVC 应用程序在登录页面上为忘记密码的用户显示了一个恢复密码链接。应用程序的第一部分工作正常,并且向用户发送了一封邮件......但后面的部分允许用户更改他们的密码不管用 。

这是我忘记密码控制器的操作方法:

[AllowAnonymous]
public ActionResult ForgotPassword()
{
    return View();
}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ForgotPassword(string UserName)
{
    //Check user existence
    var user = Membership.GetUser(UserName);

    if (user == null)
    {
        TempData["Message"] = "User Not exist.";
    }
    else
    {
        //Generate password token
        var token = WebSecurity.GeneratePasswordResetToken(UserName);

        //Create URL with above token
        var resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { un = UserName, rt = token }, "http") + "'>Reset Password</a>";

        //Get user emailid
        UsersContext db = new UsersContext();
        var emailid = (from i in db.UserProfiles
                       where i.UserName == UserName
                       select i.EmailId).FirstOrDefault();

        //Send mail
        string subject = "Password Reset Token";
        string body = "<b>Please find the Password Reset Token</b><br/>" + resetLink;

        //Edit it
        try
        {
            SendEMail(emailid, subject, body);
            TempData["Message"] = "Mail Sent.";
        }
        catch (Exception ex)
        {
            TempData["Message"] = "Error occured while sending email." + ex.Message;
        }

    }

    return View();
}

这是我忘记密码的视图:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <fieldset>
        <legend>Forgot Password Form</legend>
        <ol>
            <li>
                @Html.Label("User Name", new { @for = "UserName" })
                @Html.TextBox("UserName")
                <span style="color:red;">@TempData["Message"]</span>
            </li>
        </ol>
        <input type="submit" value="Recover" />
    </fieldset>
}

忘记密码查看页面允许用户输入他们的用户名。我得到用户名和电子邮件 ID 并构建 URL 并向用户发送邮件。这部分运行成功。现在用户被重定向到我的重置密码页面。在这里,我希望允许用户输入新密码并更新我的数据库。我的重置控制器操作和查看页面应该如何?

我什至无法使用以下控制器操作显示旧密码:(我哪里出错了?)

[AllowAnonymous]
public ActionResult ResetPassword(string un, string rt)
{
    UsersContext db = new UsersContext();

    //Get userid of received username
    var userid = (from i in db.UserProfiles
                  where i.UserName == un
                  select i.UserId).FirstOrDefault();


    var yrpassword = (from k in db.webpages_Memberships
                      where k.UserId == userid
                      select k.Password);

    TempData["Message"] = "This is your user name" + un; 
    TempData["Message"] = "This is your user-id" + userid;
    TempData["Message"] = "This is your user password" + yrpassword;

    return View();
}

这是重置密码控制器操作的当前视图页面:

@model MvcApplication1.Models.webpages_Membership

@{
    ViewBag.Title = "Reset Password";
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
</hgroup>

<section id="loginForm">

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Password Reset Form</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.Password)
                @Html.TextBoxFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </li>                           
        </ol>
        <input type="submit" value="Save" />
    </fieldset>        
        <ol>
            <li>
                @Html.ActionLink("Login", "Login") You can now login to your account.
            </li>
        </ol>
}
</section>

我知道它需要更新查询,但不知何故我无法正确处理。

4

1 回答 1

0

在 beginform 方法中给出方法和控制器名称以执行该方法。做这个:

@using (Html.BeginForm("YourMethod", "Controller"))
{
   <legend>Password Reset Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.Password)
            @Html.TextBoxFor(m => m.Password)
            @Html.ValidationMessageFor(m => m.Password)
        </li>           

    </ol>
    <input type="submit" value="Save" />

}
于 2013-06-10T09:44:13.270 回答