title: ASP.NET Identity WebAPI invalid password reset token
body: <p>I have a Webapi service that generates the password Reset token.<br>
  <strong>Token generation service end point:</strong></p>
  
  <pre><code>    [Authorize(Users = "abcd")]
      [HttpGet]
      [ActionName("GenerateForgotPasswordToken")]
      public async Task&lt;IHttpActionResult&gt; GenerateForgotPasswordToken(string key)
      {
          if (key == null || UserManager.FindById(key) == null)
          {
              return InternalServerError(new Exception("User not found"));
          }
          return Ok(await UserManager.GeneratePasswordResetTokenAsync(key));
      }
  </code></pre>
  
  <p><strong>My application UserManager:</strong></p>
  
  <pre><code>    public static ApplicationUserManager Create(IdentityFactoryOptions&lt;ApplicationUserManager&gt; options, IOwinContext context)
      {
          //var manager = new ApplicationUserManager(new UserStore&lt;ApplicationUser&gt;(context.Get&lt;ApplicationDbContext&gt;()));
          var manager = new ApplicationUserManager(new UserStore&lt;IdentityUser&gt;(new MTA()));
          // Configure validation logic for usernames
          manager.UserValidator = new UserValidator&lt;IdentityUser&gt;(manager)
          {
              AllowOnlyAlphanumericUserNames = false,
              RequireUniqueEmail = true
          };
          // Configure validation logic for passwords
          manager.PasswordValidator = new PasswordValidator
          {
              RequiredLength = 6,
              RequireNonLetterOrDigit = true,
              RequireDigit = true,
              RequireLowercase = true,
              RequireUppercase = true,
          };
          var dataProtectionProvider = options.DataProtectionProvider;
          if (dataProtectionProvider != null)
          {
              manager.UserTokenProvider = new DataProtectorTokenProvider&lt;IdentityUser&gt;(dataProtectionProvider.Create("ASP.NET Identity"));
          }
          return manager;
      }
  </code></pre>
  
  <p>The token will be used in an email to send a password reset URL to the user. That URL points to an ASP.NET MVC view which is part of my WebAPI project and obviously is hosted in the same web application in IIS. The password reset button on the page calls my other service endpoint which resets the password.<br>
  <strong>Passowrd reset service end point:</strong></p>
  
  <pre><code>    [HttpGet]
      [AllowAnonymous]
      public async Task&lt;HttpResponseMessage&gt; ResetPassword([FromUri]string email,[FromUri]string code,[FromUri]string password)
      {
          var user = await UserManager.FindByEmailAsync(email);
          var result = await UserManager.ResetPasswordAsync(user.Id, code, password);
          if (result.Succeeded)
          {
              return Request.CreateResponse();
          }
          return Request.CreateResponse(System.Net.HttpStatusCode.Ambiguous);
      }
  </code></pre>
  
  <p>Also it might help to mention that both those web api endpoints are in the same controller and in the controller I defined a global UserManger as follows:</p>
  
  <pre><code>    private ApplicationUserManager _userManager;
      public ApplicationUserManager UserManager
      {
          get
          {
              return _userManager ?? Request.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
          }
          private set
          {
              _userManager = value;
          }
      }
  </code></pre>
  
  <p>When I use an external tool like Advanced REST Client, I am able to hit the first endpoint to generate the token and then pass the token to the second endpoint along with email and new password and successfully reset the password. However when my ASP.NET MVC controller uses the same token generated by the first endpoint and call the passwordReset endpoint , the token is invalid! I already make sure that the is no Encoding/Decoding issue and the token that is received by the second end point is identical in both tests.<br>
  Once again all my WebApi and ASP controllers are in the same project and hosted in the same web application. 
  I think the problem might be related to having a new <em>Token provider</em> when a request is comming based on the <em>OwinContext</em> but I don't understand why it works calling it through web browser.</p>
  
  <pre><code> public static ApplicationUserManager Create(IdentityFactoryOptions&lt;ApplicationUserManager&gt; options, IOwinContext context)
  </code></pre>