Asp.NET Identity 2 giving “Invalid Token” error

前端 未结 21 1951
情话喂你
情话喂你 2020-11-27 03:04

I\'m using Asp.Net-Identity-2 and I\'m trying to verify email verification code using the below method. But I am getting an \"Invalid Token\"

21条回答
  •  隐瞒了意图╮
    2020-11-27 03:35

    Because you are generating token for password reset here:

    string code = UserManager.GeneratePasswordResetToken(user.Id);
    

    But actually trying to validate token for email:

    result = await UserManager.ConfirmEmailAsync(id, code);
    

    These are 2 different tokens.

    In your question you say that you are trying to verify email, but your code is for password reset. Which one are you doing?

    If you need email confirmation, then generate token via

    var emailConfirmationCode = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
    

    and confirm it via

    var confirmResult = await UserManager.ConfirmEmailAsync(userId, code);
    

    If you need password reset, generate token like this:

    var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
    

    and confirm it like this:

    var resetResult = await userManager.ResetPasswordAsync(user.Id, code, newPassword);
    

提交回复
热议问题