1

首先,我对 MVC 很陌生,我正在尝试为 MVC 4 实现密码恢复功能。我正在使用此处发布的此技术来实现此功能:在哪里可以找到 C# 示例代码以在 ASP .NET MVC2 中实现密码恢复

我了解它的工作方式,但是缺少我现在尝试实现的辅助类。我说的是类:NotificationsHelper.SendPasswordRetrieval(model.Email, this.ControllerContext);

控制器上的RetrievePassword动作控制器有一个参数PasswordRetrievalModel model。我猜这是一个连接到 db 并在主题中实现一些属性的类模型是一个名为Email的字符串属性。这个对吗?

NotificationsHelper.SendPasswordRetrieval(model.Email, this.ControllerContext); 静态类使用 2 个参数实现此静态方法SendPasswordRetrievlamodel.Email是来自 PasswordRetrievalModel 模型类的字符串属性,因此这将是我们将向其发送电子邮件的用户电子邮件。比第二个参数是this.ControllerContext。这个参数的意义何在?将包含哪些值发送到SendPasswordRetrieval方法?

比我实现的类是这样的:

public static class NotificationsHelper
{
    public static bool SendPasswordRetrieval(string emailAddress, ControllerContext ctx)
    {
        try
        {
            StringBuilder emailMessage = new StringBuilder();

            emailMessage.Append("<br />");
            emailMessage.Append("Hello,");
            emailMessage.Append("You have requested a password recovery.");
            emailMessage.Append("<br />");
            emailMessage.Append("Please click the link below to change your password: <br />");
            emailMessage.Append("<br />");
            emailMessage.Append(string.Format("http://www.example.com/Account/Validate?email={0}&token={1}", emailAddress, "**345982374532453435345**"));
            emailMessage.Append("<br />");

            MailMessage email = new MailMessage();
            email.From = new MailAddress("noreplay@example.com");
            email.To.Add(new MailAddress(emailAddress));
            email.Subject = "domain.com Password Recovery";
            email.Body = emailMessage.ToString();
            email.IsBodyHtml = true;

            SmtpClient smtpServer = new SmtpClient();
            smtpServer.Host = "smtp.gmail.com";
            smtpServer.Port = 587;
            smtpServer.Credentials = new NetworkCredential("username", "password");
            smtpServer.EnableSsl = true;
            smtpServer.Send(email);
            return true;
        }            
        catch (Exception e)
        {
            Trace.WriteLine(String.Format("Failure to send email to {0}.", emailAddress));
            return false;
        }
    }
}

在上面的代码中,我列出了格式化 url 的行,如何使用提供的代码 @agarcian 将令牌带到那里?令牌是否来自第二个参数ControllerContext?如果是,我如何从那里得到它?

4

1 回答 1

0

usertable为它添加新列pwdresetTocket,当用户请求重置密码时,Guid.NewGuid()在该用户的 pwdresetTocket 字段中插入,在回调 URL 中附加相同的内容

如果您不想将列添加到现有表,您可以创建一个新表并将其映射到用户表。

然后你的方法看起来像这样。

public static bool SendPasswordRetrieval(string emailAddress, ControllerContext ctx)
    {
        try
        {
            StringBuilder emailMessage = new StringBuilder();
          string token = Guid.NewGuid();
        // call to a method that will update the table with token
        updateUsertablewithResetTocket(tocken);

            emailMessage.Append("<br />");
            emailMessage.Append("Hello,");
            emailMessage.Append("You have requested a password recovery.");
            emailMessage.Append("<br />");
            emailMessage.Append("Please click the link below to change your password: <br />");
            emailMessage.Append("<br />");
            emailMessage.Append(string.Format("http://www.example.com/Account/Validate?email={0}&token={1}", emailAddress, token));
            emailMessage.Append("<br />");

            MailMessage email = new MailMessage();
            email.From = new MailAddress("noreplay@example.com");
            email.To.Add(new MailAddress(emailAddress));
            email.Subject = "domain.com Password Recovery";
            email.Body = emailMessage.ToString();
            email.IsBodyHtml = true;

            SmtpClient smtpServer = new SmtpClient();
            smtpServer.Host = "smtp.gmail.com";
            smtpServer.Port = 587;
            smtpServer.Credentials = new NetworkCredential("username", "password");
            smtpServer.EnableSsl = true;
            smtpServer.Send(email);
            return true;
        }            
        catch (Exception e)
        {
            Trace.WriteLine(String.Format("Failure to send email to {0}.", emailAddress));
            return false;
        }
    }

用户重置密码后,清空重置令牌字段

于 2013-01-08T21:37:37.897 回答