title: How to add the UserManager via Dependency Injection in AspNet.Core 2.1?
body: <p>I've created an Asp.Net Core 2.1 Web Api that uses Bearer Token authentication using Identity Server 4.  I have two db contexts, one that is for identity and one that is my data access for my custom application.  I want to be able to use the UserManager and UserStore in my Web Api using dependency injection in my controllers.</p>
  
  <p>The problem is that once I add in the "relevant" code for DI, my controller methods are throwing a 302 and trying to redirect me to a login URL.  It's like my bearer authentication is being overridden.  Below is what my Startup.cs looks like. Any thoughts on how this should be done?</p>
  
  <pre><code>   public void ConfigureServices(IServiceCollection services)
     {
  
          string connectionString = Configuration.GetConnectionString("DefaultConnection");
          var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
  
          services.AddDbContext&lt;CustomContext&gt;(options =&gt;
              options.UseSqlServer(connectionString));
  
          // start of the stuff that should add the usermanager and userstore
          services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseSqlServer(connectionString));
  
  
          services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
              .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
              .AddDefaultTokenProviders();
          // end of the stuff
  
          services.AddMvcCore()
              .AddAuthorization()
              .AddJsonFormatters();
  
          services.AddAuthentication("Bearer")
              .AddIdentityServerAuthentication(options =&gt;
              {
                  options.Authority = "http://authenticate.com";
                  options.RequireHttpsMetadata = false;
  
                  options.ApiName = "api1";
              });
  
          services.AddCors(options =&gt;
          {
              // this defines a CORS policy called "default"
              options.AddPolicy("default", policy =&gt;
              {
                  policy.AllowAnyOrigin()
                      .AllowAnyHeader()
                      .AllowAnyMethod()
                      .AllowCredentials();
              });
          });
  
  
      }
  
      public void Configure(IApplicationBuilder app)
      {
          app.UseAuthentication();
          app.UseCors("default");
          app.UseMvc();
      }
  </code></pre>
  
  <p>The controllers look like this:</p>
  
  <pre><code>public class PlayerController : BaseApiController
  {
      private readonly UserManager&lt;ApplicationUser&gt; userManager;
  
      public PlayerController(UserManager&lt;ApplicationUser&gt; _userManager)
      {
          userManager = _userManager;
      }
      .....
   }
  </code></pre>
  
  <p>With BaseApiController like this:</p>
  
  <pre><code>[Route("api/[controller]")]
  [Authorize]
  public class BaseApiController : Controller
  {
      ...
  }
  </code></pre>