Asp.net mvc 4 ASP.NET MVC4中的忘记密码功能

Asp.net mvc 4 ASP.NET MVC4中的忘记密码功能,asp.net-mvc-4,Asp.net Mvc 4,我正在ASP.NET MVC4中实现忘记密码功能,但无法成功允许用户重置密码 我的MVC应用程序在登录页面上为忘记密码的用户显示“恢复密码”链接。应用程序的第一部分工作正常,并向用户发送邮件……但允许用户更改密码的后一部分不起作用 这是我忘记密码的控制器操作方法: [AllowAnonymous] public ActionResult ForgotPassword() { return View(); } [HttpPost] [AllowAnonymous] [ValidateAn

我正在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();
}
这是重置密码控制器操作的当前查看页面:

@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>
@model mvcapapplication1.Models.webpages\u会员资格
@{
ViewBag.Title=“重置密码”;
}
@ViewBag.Title。
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
密码重置表格
  • @LabelFor(m=>m.Password) @Html.TextBoxFor(m=>m.Password) @Html.ValidationMessageFor(m=>m.Password)
  • @ActionLink(“Login”,“Login”)您现在可以登录到您的帐户。
  • }

    我知道它需要一个更新查询,但不知何故我无法正确执行。

    在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" />
    
    }
    
    @使用(Html.BeginForm(“您的方法”、“控制器”))
    {
    密码重置表格
    
  • @LabelFor(m=>m.Password) @Html.TextBoxFor(m=>m.Password) @Html.ValidationMessageFor(m=>m.Password)
  • }
    @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>
    
    @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" />
    
    }