忘记密码MVC 4

时间:2014-05-05 12:45:55

标签: c# asp.net-mvc linq

在我的应用程序中,我试图忘记密码才能工作。我正在尝试向用户发送电子邮件,当他/她输入他或她的用户名,发送和发送电子邮件,他们点击该链接,然后被带回网站并更改他们的密码。唯一的问题是我的linq查询是错误的,而不是检查数据库中是否存在用户名。这是忘记密码工作的正确方法吗?

这是我的代码

控制器

    // GET: /Account/ForgotPassword
    [AllowAnonymous]
    public ActionResult ForgetPassword()
    {
        return View();
    }

    // Post: /Account/ForgotPassword
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ForgetPassword(ForgetPasswordModel model, string UserName)
    {
        string emailAddress = (from i in db.UserProfiles
                                where i.UserName.Equals(model.Username)
                                select i.Email).Single();

        if (!string.IsNullOrEmpty(emailAddress))
        {
            string confirmationToken =
                WebSecurity.GeneratePasswordResetToken(model.Username);
            dynamic email = new Email("ChngPasswordEmail");
            email.To = emailAddress;
            email.UserName = model.Username;
            email.ConfirmationToken = confirmationToken;
            email.Send();

            return RedirectToAction("ResetPwStepTwo");
        }

        return RedirectToAction("InvalidUserName");
    }

模型

public class ForgetPasswordModel
{
    [Required]
    [Display(Name = "Username")]
    public string Username { get; set; }
}

查看

@model MyFestival.Models.UserProfile
@{
ViewBag.Title = "Forgot Password";
}

<hr />
<div class="form-group">
  <h3 class="panel-title">Did you forget your password?</h3>
</div>

@using (Html.BeginForm())


{
<div class="">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, null, new { @style = "color:red;" })
    <hr />
    <label>To reset your password, input your email address and press the Reset Password button.</label>
    <br/>
    <div class="form-group" >
        <div class="col-md-12" >
            <div class="input-group">
                <span class="input-group-addon" ><i class="glyphicon glyphicon-user" ></i ></span >
                @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "Username" })
            </div >
            @Html.ValidationMessageFor(m => m.UserName, null, new { @style = "color:red;" })
        </div>
    </div>
    <br/>
    <br/>
    <div class="form-group" >
        <div class="col-md-offset-2 col-md-10" >
            <input type="submit" class="btn btn-default"  value="Reset Password"/ >
            @Html.ActionLink("Back to Login", "Login", null, new { @class = "btn btn-info" })
        </div >
    </div>
</div>


}

2 个答案:

答案 0 :(得分:0)

我刚刚发现您有ForgetPasswordModel modelstring username作为控制器操作的参数,之后您使用模型的Username属性。我假设Model Binder只是初始化stirng username而不是ForgegPasswordModel。您可以尝试删除stirng username - ForgetPassword操作的第二个参数吗?

答案 1 :(得分:0)

您可以创建自己的GUID并通过电子邮件将其发送给用户。 当用户单击电子邮件链接时,选中“引导并更新用户密码”。

这是示例代码

创建视图以输入忘记密码的电子邮件

With secondary_instrument (ric, price,volume, index_for_joining)
AS
(SELECT ric,price, volume,minutesecindex
FROM  [firsttry].[dbo].[swirepacific5] where views='trdprc_1' and inst = 0 and RIC = '0087.HK'
)

SELECT TOP (1000) * from secondary_instrument

然后在Controller中从用户那里获取电子邮件ID,检查电子邮件是否存在并创建新的GUID,将其保存在数据库中,然后将电子邮件发送给用户。

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

<h2>Forgot Password</h2>

 @using (Html.BeginForm())
 {
   <div class="form-horizontal">
  <hr />
   <div class="text-success">
      @ViewBag.Message
   </div>
    <div class="form-group">
    Please enter your registered email address below to receive an email containing a link, to reset your password.
   </div>
    <div class="form-group">
       <label class="control-label col-md-2">Email Address</label>
       @Html.TextBox("EmailID", "", new { @class = "form-control" })
  </div>
 <div class="form-group">
     <div class="col-md-offset-2 col-md-10">
         <input type="submit" value="Submit" class="btn btn-success" />
    </div>
</div>
  </div>
 }

现在,当用户单击电子邮件并输入新密码时,使用我们保存在数据库中的内容检查Guid并在guid匹配时更新密码。

查看更多信息:

Forgot password functionality in ASP.NET MVC (Reset password by Email)