I have recently migrated Asp.net identity 1.0 to 2.0 . I am trying to verify email verification code using below method. But i am getting "Invalid Token" error message.
public async Task<HttpResponseMessage> ConfirmEmail(string userName, string code)
{
ApplicationUser user = UserManager.FindByName(userName);
var result = await UserManager.ConfirmEmailAsync(user.Id, code);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
Generating Email verification token using below code (And if i call ConfirmEmailAsyc immediate after generating token, which is working fine). But when i am calling using different method which is giving error
public async Task<HttpResponseMessage> GetEmailConfirmationCode(string userName)
{
ApplicationUser user = UserManager.FindByName(userName);
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
//var result = await UserManager.ConfirmEmailAsync(user.Id, code);
return Request.CreateResponse(HttpStatusCode.OK, code);
}
Please help
Hi this happened if I am getting the url(full) and calling to the api throught WebClient. The code value have to be Encoded before sending the call.
code = HttpUtility.UrlEncode(code);
I found you had to encode the token before putting it into an email, but not when checking it afterwards. So my code to send the email reads:
// 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. ";
The code confirming the email then 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 in the end for me!
Hope the issue got resolved. Otherwise below is the link for the solution which worked well.
Asp.NET - Identity 2 - Invalid Token Error
Simply use:
emailConfirmationCode = await
UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
UserManager.ConfirmEmailAsync(userId, code1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With