在使用Aspnet Core 3.1的Identity ConfirmEmail功能时遇到问题,可以尝试以下解决方法:
services.AddTransient();
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
[HttpGet]
[AllowAnonymous]
public async Task ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToAction("Error");
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return RedirectToAction("Error");
}
var result = await _userManager.ConfirmEmailAsync(user, code);
if (!result.Succeeded)
{
return RedirectToAction("Error");
}
return RedirectToAction("EmailConfirmed");
}
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
以上是一些可能导致Aspnet Core 3.1 Identity ConfirmEmail功能不如预期工作的常见问题和解决方法。根据具体情况,可能还需要进一步调试和排查其他可能的问题。