首先,我对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 model 。我猜这是一个连接到db并在主题中实现一些属性的类模型是一个名为电子邮件的字符串属性。这是对的吗?
然后, NotificationsHelper.SendPasswordRetrieval(model.Email,this.ControllerContext); 静态类使用2个paramateres实现此静态方法 SendPasswordRetrievla : 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 ?如果是的话我怎么从那里得到它?
答案 0 :(得分:0)
为usertable
添加新列{name} pwdresetTocket
,当用户请求在该用户的pwdresetTocket字段中重置密码Guid.NewGuid()
时,在回调网址中附加相同的
如果您不想将列添加到现有表,则可以创建新表并将其映射到用户表。
然后你的方法看起来像这样。
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;
}
}
用户重置密码后,清空重置令牌字段