Q:

Add Identity after ConfigureServices

I am trying to add Identity after the ConfigureServices method. It's because I have registered the dbContext outside the ConfigureServices method. So, now I have to configure Identity after configuring dbContext (Again, which I have done outside the ConfigureServices). What I have done for the dbContext is that I have created a factory and added it to the ConfigureServices:
services.AddScoped<IDbContextFactory, DbContextFactory>();

Then I have used DI to inject it in my controller's constructor:
    private IDbContextFactory contextFactory;
    private AppDbContext context;

    public DbTestController(IDbContextFactory _contextFactory)
    {
        contextFactory = _contextFactory;
        context = contextFactory.Create(); //Create() returns an `AppDbContext`
    }

I am able to perfom CRUD operations on the database but, Identity won't work, it throws and exception:
No service for type Microsoft.AspNetCore.Identity.XXX has been registered
and that's because I am registering Identity in the ConfigureServiceswithout setting up the dbContext first (Because it is set up after the ConfigureServices method.
services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>()
        .AddDefaultTokenProviders();

Is there a way to set up identity in the same manner as I have set up dbContext?
Edit: The DbContextFactory includes a Create method that returns an AppDbContext. It reads the user's choice for database provider from some config file, registers the provider accordingly and then returns the AppDbContext. Here is the code for the Create method:
public AppDbContext Create()
    {
        //Get this value from some configuration
        string providerType = _configuration.GetValue<string>("DatabaseProvider");

        //and the connection string for the database
        string connectionString = _configuration.GetConnectionString(providerType);

        var dbContextBuilder = new DbContextOptionsBuilder();

        //Add some if else blocks here to check which provider to use
        //and then call dbContextBuilder.UseABC(connectionString)
        if (providerType == "MSSQL")
            dbContextBuilder.UseSqlServer(connectionString);
        else if (providerType == "SQLite")
            dbContextBuilder.UseSqlite(connectionString);

        //Create the context
        context = new AppDbContext(dbContextBuilder);

        return context;
    }

This method reads providerType & connectionString from appsettings.json. This is what the section looks like:
"DatabaseProvider": "MSSQL", //could be MySQL, SQLite etc etc
"ConnectionStrings": {
"MSSQL": "Server=(localdb)\\MSSQLLocalDB;Database=XXX_db;Trusted_Connection=True;MultipleActiveResultSets=true",
"SQLite": "Data Source=XXX_db.db"
 }

A:

Register the context and identity as usual in the ConfigureServices, applying the desired logic in the composition root at startup.
//Get this value from some configuration
string providerType = Configuration.GetValue<string>("DatabaseProvider");

//and the connection string for the database
string connectionString = Configuration.GetConnectionString(providerType);

services.AddDbContext<AppDbContext>(options => {
    if (providerType == "MSSQL")
        options.UseSqlServer(connectionString);
    else if (providerType == "SQLite")
        options.UseSqlite(connectionString);
});

services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>()
        .AddDefaultTokenProviders();

What you need to do is redesign the factory 
public class DbContextFactory : IDbContextFactory {

    private readonly Func<AppDbContext> factory;

    public DbContextFactory(Func<AppDbContext> factory) {
        this.factory = factory;
    }

    public AppDbContext Create() {
        return factory();
    }
}

and register it accordingly with a factory delegate
services.AddScoped<IDbContextFactory, DbContextFactory>(sp => 
    new DbContextFactory(() => sp.GetRequiredService<AppDbContext>()));

