title: AspNet.Identity throws InvalidOperationException when used with SimpleInjector
body: <p>I have a simple ASP.NET vNext project, with ASP Identity and SimpleInjector configured (according to <a href="http://simpleinjector.readthedocs.org/en/latest/aspnetintegration.html" rel="nofollow">http://simpleinjector.readthedocs.org/en/latest/aspnetintegration.html</a>).</p>
  
  <p>Registration works fine, however when I try to log in I get an error:</p>
  
  <p><code>InvalidOperationException: No authentication handler is configured to handle the scheme: Microsoft.AspNet.Identity.Application</code></p>
  
  <p>If I comment out the lines for SimpleInjector everything works just fine, which seems kind of weird. Google didn't help me either :/ </p>
  
  <p>This is my Startup class.</p>
  
  <pre><code>public class Startup
  {
      //SimpleInjector IOC container
      private readonly Container _container = new Container();
  
      private IConfigurationRoot Configuration { get; }
  
      public Startup(IHostingEnvironment env)
      {
          var builder = new ConfigurationBuilder()
              .AddJsonFile("appsettings.json")
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
  
          builder.AddEnvironmentVariables();
          Configuration = builder.Build();
      }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddEntityFramework()
                  .AddSqlServer()
                  .AddDbContext&lt;MyIdentityDbContext&gt;(options =&gt; options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
  
          services.AddIdentity&lt;User, IdentityRole&gt;(options =&gt;
          {
              //TODO 
              options.Password.RequireDigit = false;
              options.Password.RequiredLength = 1;
              options.Password.RequireLowercase = false;
              options.Password.RequireNonLetterOrDigit = false;
              options.Password.RequireUppercase = false;
          }).AddEntityFrameworkStores&lt;MyIdentityDbContext&gt;().AddDefaultTokenProviders();
  
          services.AddMvc();
  
          services.AddInstance&lt;IControllerActivator&gt;(new SimpleInjectorControllerActivator(_container));
          services.AddInstance&lt;IViewComponentInvokerFactory&gt;(new SimpleInjectorViewComponentInvokerFactory(_container));
          //
          // Work around for a Identity Framework bug inside the SignInManager&lt;T&gt; class.
          services.Add(ServiceDescriptor.Instance&lt;IHttpContextAccessor&gt;(new NeverNullHttpContextAccessor()));
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
          ConfigureSimpleInjector(app);
  
          loggerFactory.AddConsole(Configuration.GetSection("Logging"));
          loggerFactory.AddDebug();
  
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
              app.UseDatabaseErrorPage();
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
          }
  
          app.UseIISPlatformHandler(options =&gt; options.AuthenticationDescriptions.Clear());
          app.UseStaticFiles();
          app.UseIdentity();
  
          app.UseMvcWithDefaultRoute();
      }
  
      private void ConfigureSimpleInjector(IApplicationBuilder app)
      {
          _container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();
  
          app.UseSimpleInjectorAspNetRequestScoping(_container);
  
          //register all of the services
          RegisterServices(app);
  
          _container.RegisterAspNetControllers(app);
          _container.RegisterAspNetViewComponents(app);
          _container.Verify();
      }
  
      /// &lt;summary&gt;
      /// Register all of the required services for IoC
      /// &lt;/summary&gt;
      /// &lt;param name="app"&gt;&lt;/param&gt;
      private void RegisterServices(IApplicationBuilder app)
      {
          //DB
          RegisterDbServices(app);
  
          //IDENTITY
          _container.CrossWire&lt;UserManager&lt;User&gt;&gt;(app);
          _container.CrossWire&lt;SignInManager&lt;User&gt;&gt;(app);
  
          //register services
          _container.CrossWire&lt;ILoggerFactory&gt;(app);
      }
  
      private void RegisterDbServices(IApplicationBuilder app)
      {
          _container.RegisterSingleton&lt;IDocumentStore&gt;(() =&gt;
          {
              var store = new DocumentStore
              {
                  Url = Configuration["Data:RavenDB:URL"]
              };
              store.Initialize();
  
              return store;
          });
          _container.Register(() =&gt;
          {
              var session = _container.GetInstance&lt;IDocumentStore&gt;().OpenAsyncSession();
              session.Advanced.UseOptimisticConcurrency = true;
              return session;
          }, Lifestyle.Scoped);
      }
  
      // Entry point for the application.
      public static void Main(string[] args) =&gt; WebApplication.Run&lt;Startup&gt;(args);
  
      /// &lt;summary&gt;
      /// Workaround for Identity bug: http://simpleinjector.readthedocs.org/en/latest/aspnetintegration.html
      /// &lt;/summary&gt;
      private sealed class NeverNullHttpContextAccessor : IHttpContextAccessor
      {
          private readonly AsyncLocal&lt;HttpContext&gt; _context = new AsyncLocal&lt;HttpContext&gt;();
  
          public HttpContext HttpContext
          {
              get { return _context.Value ?? new DefaultHttpContext(); }
              set { _context.Value = value; }
          }
      }
  }
  </code></pre>
  
  <p>The stacktrace: </p>
  
  <pre><code> System.InvalidOperationException: No authentication handler is configured to handle the scheme: Microsoft.AspNet.Identity.Application
     at Microsoft.AspNet.Http.Authentication.Internal.DefaultAuthenticationManager.d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Identity.SignInManager`1.d__27.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Identity.SignInManager`1.d__43.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.AspNet.Identity.SignInManager`1.d__30.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.AspNet.Identity.SignInManager`1.d__31.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Movies.Web.Controllers.AccountController.d__6.MoveNext() in C:\Users\mojol\Documents\C#\Web\Movies\src\Movies.Web\Controllers\AccountController.cs:line 68
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.AspNet.Mvc.Controllers.ControllerActionExecutor.d__8`1.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__53.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__44.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler.d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Routing.Template.TemplateRoute.d__27.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Routing.RouteCollection.d__9.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Builder.RouterMiddleware.d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Diagnostics.Entity.MigrationsEndPointMiddleware.d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()</code></pre>