我星期五发布了this question,因为我无法弄清楚为什么我的电子邮件没有被发送出去。今天为了深入挖掘,我在forgotpassword
方法中设置了一些断点。我发现user
变量返回null,但我不确定为什么?
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null)
{
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}
// If we got this far, something failed, redisplay form
return View(model);
}
该网站会将电子邮件地址存储在数据库中。我还希望确保我在模型中添加的一个也是用户使用的一个,它仍然返回null。在此应用程序中登录的一点是,我更改了登录页面以使用用户名而不是电子邮件。
答案 0 :(得分:1)
在线没有关于如何使用UserManager创建密码令牌的资料。对于将来遇到此问题的任何人来说,这就是它的解决方法。
1。)创建一个名为MyClasses的新文件夹并创建并添加以下类
public class GmailEmailService:SmtpClient
{
// Gmail user-name
public string UserName { get; set; }
public GmailEmailService() :
base(ConfigurationManager.AppSettings["GmailHost"], Int32.Parse(ConfigurationManager.AppSettings["GmailPort"]))
{
//Get values from web.config file:
this.UserName = ConfigurationManager.AppSettings["GmailUserName"];
this.EnableSsl = Boolean.Parse(ConfigurationManager.AppSettings["GmailSsl"]);
this.UseDefaultCredentials = false;
this.Credentials = new System.Net.NetworkCredential(this.UserName, ConfigurationManager.AppSettings["GmailPassword"]);
}
}
2。)配置您的身份类
public async Task SendAsync(IdentityMessage message)
{
MailMessage email = new MailMessage(new MailAddress("youremailadress@domain.com", "(any subject here)"),
new MailAddress(message.Destination));
email.Subject = message.Subject;
email.Body = message.Body;
email.IsBodyHtml = true;
GmailEmailService mailClient = new GmailEmailService();
await mailClient.SendMailAsync(email);
}
3。)将您的凭据添加到web.config。我没有在这部分使用gmail,因为在我的工作场所阻止了gmail的使用,它仍然可以正常运行。
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="GmailUserName" value="youremail@yourdomain.com"/>
<add key="GmailPassword" value="yourPassword"/>
<add key="GmailHost" value="yourServer"/>
<add key="GmailPort" value="yourPort"/>
<add key="GmailSsl" value="chooseTrueOrFalse"/>
<!--Smptp Server (confirmations emails)-->
</appSettings>
4。)对您的帐户管理员进行必要的更改。添加以下突出显示的代码。
编译然后运行。干杯!