要在ASP.NET Core Identity中实现电子邮件确认功能,可以按照以下步骤操作:
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
app.UseAuthentication();
app.UseAuthorization();
EmailConfirmation
的控制器,并在其中添加以下代码:[AllowAnonymous]
public class EmailConfirmationController : Controller
{
private readonly UserManager _userManager;
public EmailConfirmationController(UserManager userManager)
{
_userManager = userManager;
}
[HttpGet]
public async Task ConfirmEmail(string userId, string token)
{
if (userId == null || token == null)
{
return RedirectToAction("Index", "Home");
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound($"Unable to load user with ID '{userId}'.");
}
var result = await _userManager.ConfirmEmailAsync(user, token);
if (result.Succeeded)
{
return View("ConfirmEmail");
}
return RedirectToAction("Index", "Home");
}
}
EmailConfirmation
的文件夹,然后在其中创建一个名为ConfirmEmail.cshtml
的视图文件。在该文件中添加以下代码,以显示确认电子邮件成功的消息:Email Confirmed
Your email has been confirmed. You can now login.
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Action("ConfirmEmail", "EmailConfirmation", new { userId = user.Id, token = token }, Request.Scheme);
// 发送邮件给用户,包含上面的confirmationLink
return RedirectToAction("Index", "Home");
通过以上步骤,您就可以实现ASP.NET Core Identity中的电子邮件确认功能,并根据需要进行自定义。