MVC 5 Identity 2.0注册邮件确认问题
本文关键字:确认 问题 注册 Identity MVC | 更新日期: 2023-09-27 18:02:50
有人可以看看下面的代码,看看是什么原因导致错误"在处理您的请求时发生了错误"?我用的是ASP。. NET Identity 2.0,请参阅下面的代码:
IdentityConfig
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var credentialUserName = "info@ourdoamin.com";
var sentFrom = "noreply@ourdoamin.com";
var pwd = "ourpassword";
// Configure the client:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.ourdoamin.com");
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
// Creatte the credentials:
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(credentialUserName, pwd);
client.EnableSsl = false;
client.Credentials = credentials;
// Create the message:
var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
return Task.FromResult(0);
}
}
显然,出于安全原因,我没有在上面的代码中使用正确的凭据,但是我在同一个项目中有一个使用正确凭据的联系表单,并且电子邮件工作得很好。
如果我注释掉上面的代码,除了public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { return Task.FromResult(0); } }
,注册过程成功完成。
任何帮助将不胜感激:-)
问题是行return Task.FromResult(0);
它应该是return client.SendMailAsync(mail);
代替,现在一切都在工作:-)
使用await在我看来是更正确的(至少对于identity>= 2.0版本
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSMTPasync(message);
}
// send email via smtp service
private async Task configSMTPasync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var credentialUserName = "info@ourdoamin.com";
var sentFrom = "noreply@ourdoamin.com";
var pwd = "ourpassword";
// Configure the client:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.ourdomain.com");
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
// Creatte the credentials:
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(credentialUserName, pwd);
client.EnableSsl = false;
client.Credentials = credentials;
// Create the message:
var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
await client.SendMailAsync(mail);
}
}