title: "Cannot access a disposed object" after minor code modification
body: <p>Using ASP.NET MVC5, EF6 &amp; Ninject as Backend, AngularJS as frontend with token based auth (JWT).</p>
  
  <p>We recently had to enable @ chars in the user names. Base on <a href="https://stackoverflow.com/questions/19460078/configure-microsoft-aspnet-identity-to-allow-email-address-as-username">this answer</a> in the Startup.cs (called by Ninject registration code, see below), we replaced </p>
  
  <pre><code>UserManagerFactory = () =&gt; new ApplicationUserManager(new UserStore&lt;IdentityUser&gt;(new SecurityDbContext()));
  </code></pre>
  
  <p>with</p>
  
  <pre><code>var userManager = new ApplicationUserManager(new UserStore&lt;IdentityUser&gt;(new SecurityDbContext()));
  var validator = new UserValidator&lt;IdentityUser&gt;(userManager)
  {
      AllowOnlyAlphanumericUserNames = false
  };
  userManager.UserValidator = validator;
  UserManagerFactory = () =&gt; userManager;
  </code></pre>
  
  <p>This allows the registration of user name with @ signs as wished. However, logging into the application (even with "normal" usernames), became buggy: While the first login after start of the server works as usual, any subsequent login creates the following exception:</p>
  
  <pre><code>Cannot access a disposed object.
  Object name: 'ApplicationUserManager'.
  </code></pre>
  
  <p>Detailed error message:</p>
  
  <pre>
  Source Error: 
  Line 18:         public override async Task FindAsync(string userName, string password)
  Line 19:         {
  Line 20:             var result = await base.FindAsync(userName, password);
  Line 21:             if (result == null)
  Line 22:             {
  
  Source File: 
  xyz\Infrastructure\ApplicationUserManager.cs    Line: 20 
  
  Stack Trace: 
  
  [ObjectDisposedException: Cannot access a disposed object.Object name: 'ApplicationUserManager'.]   Microsoft.AspNet.Identity.UserManager`1.ThrowIfDisposed() +99   Microsoft.AspNet.Identity.d__15.MoveNext() +128   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24   xyz.Infrastructure.d__0.MoveNext() in xzy\Infrastructure\ApplicationUserManager.cs:20   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24   xyz.Infrastructure.d__0.MoveNext() in xyz\Infrastructure\ApplicationOAuthProvider.cs:39   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21   Microsoft.Owin.Security.OAuth.d__3a.MoveNext() +862   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24   Microsoft.Owin.Security.OAuth.d__1e.MoveNext() +2335   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21   Microsoft.Owin.Security.OAuth.d__0.MoveNext() +1728   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24   Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +664   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21   Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +937   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21   Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +937   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22   Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +33   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +150   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +42   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
  </pre>
  
  <p>I guess it has something to do with Ninject, so here's a bit more background: the above changed code is called from the NinjectWebCommon.cs:</p>
  
  <pre><code>kernel.Bind&lt;IUserService&gt;()
      .To&lt;UserService&gt;()
      .WithConstructorArgument("userManager", Startup.UserManagerFactory());
  </code></pre>