gpt4 book ai didi

c# - UserManager.SendEmailAsync 挂起

转载 作者:行者123 更新时间:2023-11-30 14:30:14 25 4
gpt4 key购买 nike

我有一个带有自定义电子邮件服务的自定义用户管理器。我在 AccountController 中调用 UserManager.SendEmailAsync() 方法,它只是挂起。我什至尝试将它与无效的 SMTP 主机名一起使用,然后发生异常,在调试中我可以看到它进入 catch block ,但无论 return View(model) 语句如何,它只是“挂起”在浏览器中并继续加载永远。

有什么想法吗?

ApplicationUserManager 构造函数:

public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
EmailService = new EmailService();
}

电子邮件服务:

public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
// Credentials:
string smtpServer = ConfigurationManager.AppSettings["EmailSmtpServer"];
int smtpPort = int.Parse(ConfigurationManager.AppSettings["EmailSmtpPort"]);
bool enableSsl = bool.Parse(ConfigurationManager.AppSettings["EmailEnableSSL"]);
string smtpUsername = ConfigurationManager.AppSettings["EmailSmtpUsername"];
string smtpPassword = ConfigurationManager.AppSettings["EmailSmtpPassword"];
string sentFrom = ConfigurationManager.AppSettings["EmailSentFrom"];

// Configure the client:
var client = new SmtpClient(smtpServer, Convert.ToInt32(587));

client.Port = smtpPort;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = enableSsl;

// Create the credentials:
var credentials = new NetworkCredential(smtpUsername, smtpPassword);
client.Credentials = credentials;

// Create the message:
var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);

mail.Subject = message.Subject;
mail.Body = message.Body;

// Send:
await client.SendMailAsync(mail);
}
}

AccountController 中的 ForgotPassword 方法

    //
// POST: /Account/ForgotPassword
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.Email);
// Don't check confirmation status for now
//if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
if (user == null)
{
ModelState.AddModelError("", "The user either does not exist or is not confirmed.");
return View();
}

// 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);
try
{
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
return View(model);
}
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}

// If we got this far, something failed, redisplay form
return View(model);
}

最佳答案

这对我有用

ApplicationUserManager 构造函数:

public AppUserManager(IUserStore<AppUser> store)
: base(store)
{
this.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<AppUser, string>();
this.EmailService = new EmailService();
}

电子邮件服务:

public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Credentials:
var credentialUserName = ConfigurationManager.AppSettings["emailFrom"];
var sentFrom = ConfigurationManager.AppSettings["emailFrom"];
var pwd = ConfigurationManager.AppSettings["emailPassword"];

// Configure the client:
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("smtp-mail.outlook.com");

client.Port = 587;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

// Create the credentials:
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(credentialUserName, pwd);

client.EnableSsl = true;
client.Credentials = credentials;

// Create the message:
var mail =
new System.Net.Mail.MailMessage(sentFrom, message.Destination);

mail.Subject = message.Subject;
mail.Body = message.Body;

// Send:
return client.SendMailAsync(mail);
}
}

重要 如果您使用外部邮件提供商,您应该修改您的外部应用程序配置,例如:电子邮件:Allowing less secure apps to access your account

AccountController 中的 ForgotPassword 方法

    [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> RecoveryPassword(LoginInfoModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(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");
}

var 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",
"Reinicia tu contraseña clicando : <a href=\"" + callbackUrl + "\">aqui</a>");
return View("ForgotPasswordConfirmation");
}

// If we got this far, something failed, redisplay form
return View(model);
}

最后我在我的邮箱里看到了我的电子邮件

Mailbox

关于c# - UserManager.SendEmailAsync 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23954489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com