Async Sending Email Does Not Return View

I have a problem with a password link for a forgotten password on my Asp.net website.

In principle, everything works fine, it sends an email to the account, and the password can get reset, but I get a 404 error, and not return the correct page.

public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByEmailAsync(model.Email);

        if (user == null) // || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
            // 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);
}

I think this is the problem with this line:

await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

Without this line, it will return the correct view, but, obviously, the letter will not be sent. I debugged and passed through it, but could not find any errors.

Has anyone else come across this?

Thank you in advance

Nb if the model is null, it returns the correct form

EDIT: Person Report

public Task SendAsync(IdentityMessage message)
{
    // Plug in your email service here to send an email.
    var mailMessage = new MailMessage("Email here",
        message.Destination,
        message.Subject,
        message.Body
        );

    var client = new SmtpClient();
    client.SendAsync(mailMessage, null);

    return Task.FromResult(0);
}
+4
source share
1 answer

" , ". , 404, .

public Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        var mailMessage = new MailMessage("Email here",
            message.Destination,
            message.Subject,
            message.Body
            );

        var client = new SmtpClient();
        return client.SendMailAsync(mailMessage);
    }

await/async

public async Task SendAsync(IdentityMessage message)
        {

            // Plug in your email service here to send an email.
            var mailMessage = new MailMessage("Email here",
                message.Destination,
                message.Subject,
                message.Body
                );

            var client = new SmtpClient();
            await client.SendMailAsync(mailMessage);
        }
+1

Source: https://habr.com/ru/post/1624854/


All Articles