提供邮件确认和密码重置的合适服务是什么?我试过SMTP,但六次尝试后,实际上它认为这些电子邮件是垃圾邮件,并被永久阻止,sendgrid不给我访问服务。当用户单击注册表单中的注册按钮时,帐户将自动创建,而无需发送电子邮件,但当管理员试图注册新用户或管理员时,它会很好地工作。
下面是email sender类:
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var fromMail = "";
var fromPassword = "";
var message = new MailMessage();
message.From = new MailAddress(fromMail);
message.Subject = subject;
message.To.Add(email);
message.Body = $"<html><body>{htmlMessage}</body></html>";
message.IsBodyHtml = true;
var smtpClient = new SmtpClient(host:"smtp-mail.outlook.com")
{
Port = 587,
Credentials = new NetworkCredential(fromMail, fromPassword),
EnableSsl = true,
};
smtpClient.Send(message);
}
这里是Register.cshtml.cs
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = Input.Email,
Email = Input.Email,
Name=Input.Name,
PhoneNumber=Input.PhoneNumber,
StreetAddress=Input.StreetAddress,
City=Input.City,
Governorate=Input.Governorate,
PostalCode=Input.PostalCode
};
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
//determine role of new user account
if(! await _roleManager.RoleExistsAsync(SD.AdminUser))
{
await _roleManager.CreateAsync(new IdentityRole(SD.AdminUser));
}
if (!await _roleManager.RoleExistsAsync(SD.EndUser))
{
await _roleManager.CreateAsync(new IdentityRole(SD.EndUser));
}
string role = HttpContext.Request.Form["rdUserRole"].ToString();
if (string.IsNullOrEmpty(role))
{
await _userManager.AddToRoleAsync(user, SD.EndUser);
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
else
{
await _userManager.AddToRoleAsync(user, role);
}
_logger.LogInformation("User created a new account with password.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
}
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
下面是registerconfirmation.cshtml.cs:
public async Task<IActionResult> OnGetAsync(string email, string returnUrl = null)
{
if (email == null)
{
return NotFound();
}
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
return NotFound($"Unable to load user with email '{email}'.");
}
Email = email;
DisplayConfirmAccountLink = true;
if (DisplayConfirmAccountLink)
{
var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
EmailConfirmationUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
}
return Page();
}
我检查了你的代码,似乎你需要移动代码:
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
:
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
}
在您的逻辑中,if" _usermanager . options . sign . requireconfirmedaccount "默认设置为true,您将永远不会发送确认电子邮件
你可以阅读与你的代码相关的官方文件:
https://learn.microsoft.com/en us/aspnet/core/security/authentication/identity?view=aspnetcore - 6.0,标签= visual studio # examine-register