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
We had the same issue, Load balancing was causing this problem. Adding a
<machineKey validationKey="XXX" decryptionKey="XXX" validation="SHA1" decryption="AES"/>
in web.config file solved the problem. All your servers need to have the same machine key to verify previously generated code.Hope this helps.
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:
The code confirming the email then reads:
Worked in the end for me!
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.
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:
My issue was slightly different.
I created my own IUserStore and one thing I was doing wrong was setting the SecurityStamp to null if there was no value.
The security stamp is used to generate the token but it's replaced by an empty string when the token is generated, however it is not replaced when validating the token, so it ends up comparing
String.Empty
tonull
, which will always return false.I fixed my issue by replacing null values for
String.Empty
when reading from the database.Had the same issue. The fix was to HTML encode the token when generating the link, and when confirming - HTML decode it back.