I found that you needed to encode the token before placing it in the email, but not when checking it. So my code to send an email is:
// Send an email with this link string code = UserManager.GenerateEmailConfirmationToken(user.Id); // added HTML encoding string codeHtmlVersion = HttpUtility.UrlEncode(code); // for some weird reason the following commented out line (which should return an absolute URL) returns null instead // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string callbackUrl = "(your URL)/Account/ConfirmEmail?userId=" + user.Id + "&code=" + codeHtmlVersion; // Send an email with this link using class (not shown here) var m = new Email(); m.ToAddresses.Add(user.Email); m.Subject = "Confirm email address for new account"; m.Body = "Hi " + user.UserName + dcr + "You have been sent this email because you created an account on our website. " + "Please click on <a href =\"" + callbackUrl + "\">this link</a> to confirm your email address is correct. ";
Then the email confirmation code reads:
// user has clicked on link to confirm email [AllowAnonymous] public async Task<ActionResult> ConfirmEmail(string userId, string code) { // email confirmation page // don't HTTP decode // try to authenticate if (userId == null || code == null) { // report an error somehow } else { // check if token OK var result = UserManager.ConfirmEmail(userId, code); if (result.Succeeded) { // report success } else { // report failure } }
Worked, after all, for me!
source share