title: Asp.net Core Email confirmation sometimes says InvalidToken
body: <p>I am using asp.net core identity 2.1 and i am having a random issue with email confirmation, which while email confirmation sometimes says <strong>result.Error = InvalidToken</strong>. The token is also not expired.</p>
  
  <p><strong>Note:</strong> We are using <strong>multiple servers</strong>, and we have also stored our keys in one place so that all the servers use the same keys.</p>
  
  <p>Code snippet for email confirmation.</p>
  
  <p><strong>Email Confirmation</strong></p>
  
  <pre><code>var confCode = await _userManager.GenerateEmailConfirmationTokenAsync(user);
          var callbackUrl = Url.Action("ConfirmEmail", "Account", new
          {
              userId = user.Id,
              code = WebUtility.UrlEncode(confCode)
          }, protocol: HttpContext.Request.Scheme);
  
          string confirmationEmailBody = string.Format(GetTranslatedResourceString("ConfirmationEmailBody"), "&lt;a href='" + callbackUrl + "'&gt;") + "&lt;/a&gt;";
  </code></pre>
  
  <p><strong>Verification of token</strong></p>
  
  <pre><code>public async Task&lt;bool&gt; ConfirmEmailAsync(string userId, string code)
      {
          if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(code))
              return false;
  
  
          var user = await _userManager.FindByIdAsync(userId);
  
          if (user == null)
              return false;
  
          var result = await _userManager.ConfirmEmailAsync(user, code).ConfigureAwait(false);
  
          if (!result.Succeeded)
              result = await _userManager.ConfirmEmailAsync(user, WebUtility.UrlDecode(code)).ConfigureAwait(false);
  
          return result.Succeeded;
      }
  </code></pre>
  
  <p><strong>Invalid Token</strong></p>
  
  <p>The below token is encoded twice but we handle that situation</p>
  
  <pre><code>CfDJ8HYrrpCgcr5GvrItPOWapXRy8WF8odd%252BVKuDup7buRsl1x4agRpfgQlEIPWiBqM0Wuilu9tCv5l%252B3lNaAb89%252Fi%252B4k0y%252FH0jdXAbabz0%252FXDGA0eUrmcKdIsDFNuXeyP5ezTVmTx8t0ky9xCTXaKLAfvTsCJviETk5Ag9JbUs3l3%252BnUon6fyYOHsslJI5VKLqhMM0Sm%252BW1EE%252B%252FPEJ%252BXcn%252FPS1My%252BI1lExuF1R1hFEZScEsUCG%252Bx%252BVIFB9bzs1IoLC%252Baw%253D%253D
  </code></pre>
  
  <p>Any help will be appreciated, Thank you!</p>