title: ASP.NET Identity - Reset a list of password asynchronously
body: <p>I am trying to set/reset a large list of user passwords asynchronously in Identity 2.1.  When I run the process, it seems to get confused with the ApplicationUserManager.  (e.g. Even though I pass a new ID &amp; password into a new thread, I get an error saying that it is trying to change the user when it has not finished the previous one.)  Since the Current Context doesn't run in a new thread, I was passing the ApplicationUserManager to the procedure doing the actual reset.  When I run it as shown (synchronously), it works fine, but it takes 2-3 second for each user.  That means running for 8-10 hours.
    If I could instantiate the ApplicationUserManager within the ProcessUser routine, I think the problem would go away, but so far I have not been able to since there is no context related to the page.
    Any fixes?</p>
  
  <pre><code> Private Sub ProcessUsers()
      Dim mgr = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)() ' As New ApplicationUserManager()
      Dim ctx As New BCRContext()
      Dim ttl As Integer = 0
      Dim status As String
  
      ' Turn off password requirements for these temp passwords.
      mgr.PasswordValidator = New PasswordValidator() With {
          .RequireDigit = False,
          .RequireLowercase = False,
          .RequiredLength = 4,
          .RequireUppercase = False,
          .RequireNonLetterOrDigit = False
      }
  
      ' Get the list of user ID's and temp passswords 
      Dim qry As List(Of MyStuffDTO) = ctx.Database.SqlQuery(Of MyStuffDTO)("SELECT tmpPass.ID, Pass FROM tmpPass INNER JOIN AspNetUsers U ON U.ID=tmpPass.ID WHERE U.PasswordHash IS NULL ").ToList()
  
      Try
  
          For Each guy As MyStuffDTO In qry
              ttl += 1
              status = ttl &amp; "/" &amp; qry.Count &amp; " - "
  
              ProcessUser(guy, mgr, status)
  
              'Dim task__1 = Task(Of String).Factory.StartNew(Function()
              '                                                   Return ProcessUser(guy, mgr, status)
              '                                               End Function
              '    )
          Next
  
          Debug.WriteLine("Done assigning tasks")
  
      Catch ex As Exception
          Logger.Error("Error processing guys", ex)
      End Try
  End Sub
  
  Private Function ProcessUser(guy As MyStuffDTO, mgr As ApplicationUserManager, status As String) As String
      Dim rslt As IdentityResult = Nothing
      Dim msg As String = ""
  
      Try
          If mgr.HasPassword(guy.ID) Then
              rslt = mgr.RemovePassword(guy.ID)
              If Not rslt.Succeeded Then
                  msg = status &amp; "Error removing password for ID:" &amp; guy.ID &amp; " - " &amp; rslt.Errors(0)
                  Debug.WriteLine(msg)
                  Logger.Warn(msg)
                  Return msg
              End If
          End If
          rslt = mgr.AddPassword(guy.ID, guy.Pass)
          If Not rslt.Succeeded Then
              msg = status &amp; "Error setting password for ID:" &amp; guy.ID &amp; " - " &amp; rslt.Errors(0)
              Debug.WriteLine(msg)
              Logger.Warn(msg)
          Else
              msg = status &amp; "Succeeded for ID:" &amp; guy.ID
              Debug.WriteLine(msg)
          End If
  
      Catch ex As Exception
          msg = "Error progessing guy " &amp; guy.ID
          Logger.Error("Error progessing guy " &amp; guy.ID, ex)
          Return msg &amp; " - " &amp; ex.Message
      End Try
  
      Return msg
  
  End Function
  
  Public Class MyStuffDTO
      Public Property ID As String
      Public Property Pass As String
  End Class
  </code></pre>