public async Task ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToAction("Index", "Home");
}
var user = await UserManager.FindByIdAsync(userId);
if (user == null)
{
throw new ApplicationException($"Unable to load user with ID '{userId}'.");
}
var result = await UserManager.ConfirmEmailAsync(user, code);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
public async Task Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);
_logger.LogInformation("User created a new account with password.");
await SignInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation("User created a new account with password.");
return LocalRedirect(returnUrl);
}
AddErrors(result);
}
return View(model);
}
如果上述步骤均未出现问题,则可以检查一下正常情况下的邮件确认过程中身份验证需要的某些程序是否存在(例如AspNetUsers表格以及相关的存储过程等等)。
检查SMTP的配置是否正确。最好使用SmtpClient,以便您可以更好地检查SmtpException异常。
using (SmtpClient client = new SmtpClient())
{
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("yourEmail@gmail.com", "yourGmailPassword"); //Provide your email id
client.Host = "smtp.gmail.com"; //Or your SMTP Server Address
client.Port = 587;
MailMessage mailMessage = new