Search code examples
asp.net-coreasp.net-core-mvcasp.net-core-identity.net-9.0

After upgrade from .NET 8 to .NET 9 EmailConfirmation is not working


I have code like this when a user registers:

var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("confirmemail", "home", new { userId = user.Id, code }, HttpContext.Request.Scheme);

// This code in here it is working
var result2 = await _userManager.ConfirmEmailAsync(user, code);

But when I copy the callbackUrl and call from the browser, it is not able to confirm the user.

var user = await _userManager.FindByIdAsync(userId);

if (user == null)
    return View("Error");

var result = await _userManager.ConfirmEmailAsync(user, code);

Has anyone encountered this issue after upgrade from .NET 8 to .NET 9?


Solution

  • I notice, we need to UrlEncode the code before sending the email

    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
    var encodedCode = HttpUtility.UrlEncode(code);
    var callbackUrl = Url.Action("confirmemail", "home", new { userId = user.Id, code = encodedCode }, HttpContext.Request.Scheme);