[英]ASP.NET Core 3.1 create a service to send email confirmation
I call the method EmailConfirmation
on several pages so I decided to create a service for this.我在多个页面上调用了
EmailConfirmation
方法,因此我决定为此创建一个服务。
But I have a problem with GetEmailConfirmCallback
: Url
is null.但我对
GetEmailConfirmCallback
有疑问: Url
是 null。
I would like to change Identity area pages also and use this service instead of in some pages like register and externalLogin,... each time do the same task.我也想更改身份区域页面并使用此服务,而不是在注册和外部登录等某些页面中使用此服务...每次都执行相同的任务。
This is my class:这是我的 class:
public class ApplicationServices : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly IEmailSender _emailSender;
private readonly IWebHostEnvironment _webHostEnvironment;
public ApplicationServices(IEmailSender emailSender, UserManager<ApplicationUser> userManager, IWebHostEnvironment webHostEnvironment)
{
_emailSender = emailSender;
_userManager = userManager;
_webHostEnvironment = webHostEnvironment;
}
public async Task<ResultModel> EmailConfirmation(ApplicationUser user, string returnUrl = "/Identity/Account/Login")
{
ResultModel resultModel = new ResultModel();
try
{
var pathToEmailTemplate = _webHostEnvironment.WebRootPath
+ Path.DirectorySeparatorChar.ToString()
+ "templates"
+ Path.DirectorySeparatorChar.ToString()
+ "email_templates"
+ Path.DirectorySeparatorChar.ToString()
+ "confirm-email-reset-password.html";
string emailTemplateString = string.Empty;
using (StreamReader SourceReader = System.IO.File.OpenText(pathToEmailTemplate))
{
emailTemplateString = SourceReader.ReadToEnd();
}
string messageSubject = "Confirm your email";
string messageBody = $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(await GetEmailConfirmCallback(user, returnUrl))}'>clicking here</a>.";
string messageSenderName = "Support team.";
string emailMessage = string.Format(emailTemplateString, messageSubject, string.IsNullOrEmpty(user.FullName) ? user.FullName : "User", messageBody, messageSenderName);
await _emailSender.SendEmailAsync(user.Email, messageSubject, emailMessage);
resultModel.IsSucceed = true;
return resultModel;
}
catch (Exception e)
{
resultModel.IsSucceed = false;
resultModel.Message = e.Message;
return resultModel;
}
}
private async Task<string> GetEmailConfirmCallback(ApplicationUser user, string returnUrl)
{
var codeEmail = await _userManager.GenerateEmailConfirmationTokenAsync(user);
codeEmail = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(codeEmail));
var callbackUrlEmail = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = codeEmail, returnUrl },
protocol: Request.Scheme);
return callbackUrlEmail;
}
}
Any advice or assistance would be greatly appreciated.任何建议或帮助将不胜感激。
Have you tried using Url.Action
instead of Url.Page
?您是否尝试过使用
Url.Action
而不是Url.Page
? Url.Action
is for MVC and Url.Page
is for Razor. Url.Action
用于 MVC, Url.Page
用于 Razor。 You are not specifying if you are using Razor or MVC.您没有指定您使用的是 Razor 还是 MVC。
HttpContextAccessor
in Startup.csHttpContextAccessor
services.AddHttpContextAccessor();
IHttpContextAccessor
and LinkGenerator
as dependency injections in the service classIHttpContextAccessor
和LinkGenerator
作为依赖注入 classprivate readonly UserManager<ApplicationUser> _userManager;
private readonly IEmailSender _emailSender;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly LinkGenerator _generator;
public ApplicationServices(UserManager<ApplicationUser> userManager,
IEmailSender emailSender,
IWebHostEnvironment webHostEnvironment,
IHttpContextAccessor httpContextAccessor,
LinkGenerator generator)
{
_userManager = userManager;
_emailSender = emailSender;
_webHostEnvironment = webHostEnvironment;
_httpContextAccessor = httpContextAccessor;
_generator = generator;
}
GetEmailConfirmCallback
GetEmailConfirmCallback
private async Task<string> GetEmailConfirmCallback(ApplicationUser user, HttpContext context, string returnUrl)
{
var codeEmail = await _userManager.GenerateEmailConfirmationTokenAsync(user);
codeEmail = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(codeEmail));
var callbackUrlEmail = _generator.GetUriByPage(_httpContextAccessor.HttpContext,
page:"/Account/ConfirmEmail",
handler: null,
values: new { area = "Identity", userId = user.Id, code = codeEmail, returnUrl });
return callbackUrlEmail;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.