ASP.NET MVC4中的忘记密码功能

时间:2013-06-10 09:32:54

标签: asp.net-mvc-4

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

我的MVC应用程序在忘记密码的用户的登录页面上显示RECOVER密码链接。应用程序的第一部分工作正常,邮件发送给用户...但后面的部分允许用户更改密码无效。

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

[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>

我知道它需要更新查询,但不知怎的,我无法做到正确。

1 个答案:

答案 0 :(得分: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" />

}