【问题标题】:MVC 4 Password RecoveryMVC 4 密码恢复
【发布时间】:2012-11-24 16:09:33
【问题描述】:

首先,我对 MVC 很陌生,我正在尝试为 MVC 4 实现密码恢复功能。我正在使用此处发布的此技术来实现此功能:Where to find C# sample code to implement password recovery in ASP .NET MVC2

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

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

然后,NotificationsHelper.SendPasswordRetrieval(model.Email, this.ControllerContext); 静态类实现了这个静态方法 SendPasswordRetrievla 有 2 个参数:model.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?如果是,我如何从那里得到它?

【问题讨论】:

    标签: asp.net-mvc controller asp.net-mvc-4 asp.net-membership asp.net-mail


    【解决方案1】:

    usertable添加新列,将其命名为pwdresetTocket,当用户请求重置密码时,在该用户的pwdresetTocket字段中插入Guid.NewGuid(),在回调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;
            }
        }
    

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

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 2011-06-13
      • 2010-11-30
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多