ASP.NETCoreMVC中使用Identity方法来确认emails时无法获取确认码
创始人
2024-09-18 03:00:41
0
  1. 确保你在使用正确的代码进行了email的confirmation。具体参考:
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");
}
  1. 确保该email已被发送成功并且确认链接/确认码没有失效。可以添加一些日志来辅助调试:
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);
}
  1. 如果上述步骤均未出现问题,则可以检查一下正常情况下的邮件确认过程中身份验证需要的某些程序是否存在(例如AspNetUsers表格以及相关的存储过程等等)。

  2. 检查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

相关内容

热门资讯

德州之星辅助!aa poker... 《德州之星辅助!aa poker有没有挂,(nzt德州)竟然真的有挂(详细辅助新版2025教程)》 ...
透视软件(德州之星有辅助挂)德... 透视软件(德州之星有辅助挂)德州机器人软件(详细辅助分享教程)原生真的是有挂1、超多福利:超高返利,...
透视模拟器(wEpoKe)透视... 透视模拟器(wEpoKe)透视辅助安装(wepokeai代打的胜率)总是存在有挂(详细透视2025新...
智星德州菠萝!aapoker有... 智星德州菠萝!aapoker有什么规律吗,(wEpOke)果然真的是有挂(详细辅助黑科技教程);亲真...
透视实锤(wepok软件透明挂... 透视实锤(wepok软件透明挂)we poke软件免费版(详细辅助微扑克教程)本然存在有挂wepok...
透视讲解(WePoKer)透视... 透视讲解(WePoKer)透视辅助插件(wepoke的确有挂)素来是真的有挂(详细透视教你攻略)准备...
aapoker发牌机制!aap... aapoker发牌机制!aapoker是正规的吗,(微扑克)先前是真的有挂(详细透视详细教程);致您...
透视免费(wepokeai代打... 透视免费(wepokeai代打)德州ai人工智能软件下载(详细辅助透明挂教程)本来真的有挂1.德州a...
透视存在(wePOke)透视辅... 透视存在(wePOke)透视辅助工具(wepoke真的有挂)一向是真的有挂(详细透视科技教程);we...
aapoker辅助工具存在!w... aapoker辅助工具存在!wepoke有挂吗网上靠谱吗,(WepoKe)确实真的是有挂(详细辅助科...