0

有谁知道为什么这会发生在我身上?

在这种情况下,“结果”是“成功”

public async Task<IActionResult> TestConfirmInSameRequest(string userId)
{
    var user = await this._userManager.FindByIdAsync(userId);
    var code = await this._userManager.GenerateEmailConfirmationTokenAsync(user);
    var result = await this._userManager.ConfirmEmailAsync(user, code); 

    var newLocation = ...
    return Redirect(newLocation);
}

在这种情况下,“结果”始终是“无效令牌”(即使我手动复制原始代码并使用它进行测试)

public async Task<IActionResult> ConfirmEmail(string userId, string code)
{
    var user = await this._userManager.FindByIdAsync(userId);
    var result = await this._userManager.ConfirmEmailAsync(user, code); 

    var newLocation = ...;
    return Redirect(newLocation);
}

protected async Task SendConfirmationEmail(string userId, bool originMobile)
{
    var user = await this._userManager.FindByIdAsync(userId);
    var code = await this._userManager.GenerateEmailConfirmationTokenAsync(user);

    var encodedCode = HttpUtility.UrlEncode(code);
    var callbackUrl = $"https://.../api/account/confirmemail?userId={userId}&code={encodedCode}";

    await this._userService.SendConfirmationEmailAsync(userId, callbackUrl);
}
4

1 回答 1

0

当发送(SendConfirmationEmail)电子邮件给你urlencode令牌时,但在 ConfirmEmail 中你不是decoding令牌。

对其进行编码只是确保它可以在 URL 中使用并且 URL 中没有中断字符。但是,您应该验证的令牌不是编码的令牌,它仍然是您在编码之前获得的令牌。换句话说; 您需要再次解码令牌,使其恢复到生成时的状态。

于 2019-03-08T12:50:02.910 回答