1

My issue is fairly similar to this question: Using AddPooledDbContextFactory with AspNetCore.Identity. But while it helped my issue is not the same.

I have an API that uses Duende IdentityServer and AspNetCore.Identity. I use dataprotection to protect my users data in the database. However I also want to use GraphQL to expose a list of my users etc. The Hot Chocolate GraphQL package required that you use a pooled dbContext because it will sometimes execute multiple queries at once and this could cause an error in a normal dbContext registration.

When I use AddPooledDbContextFactory, however, I can't seem to get the AspNetCoreIdentity to run properly in combination with DataProtection. (It did work with a normal AddDbContext<>)

Here is my code:

var redis = ConnectionMultiplexer.Connect(configurationOptions);
services.AddSingleton(redis);
services.AddDataProtection().SetApplicationName("app").PersistKeysToStackExchangeRedis(redis, "DataProtectionKeys");
services.AddScoped<ILookupProtectorKeyRing, KeyRing>();
services.AddScoped<ILookupProtector, LookupProtector>();
services.AddScoped<IPersonalDataProtector, PersonalDataProtector>();

// some other code 
    
services.AddPooledDbContextFactory<IdentityContext>((ctx) => ctx.UseNpgsql(dataOptions.ConnectionString));
services.AddScoped<IdentityContext>(p => p.GetRequiredService<IDbContextFactory<IdentityContext>>().CreateDbContext());
services.AddIdentity<User, IdentityRole>(options => {
    options.Stores.ProtectPersonalData = true;
    options.Stores.MaxLengthForKeys = 128;
})
    .AddEntityFrameworkStores<IdentityContext>()
    .AddDefaultTokenProviders();

The error I receive is this :

Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.IPersonalDataProtector' from root provider. 

Can anyone provide me with some insight how to fix this?

Edit: I figured that I would dive into the DI of DataProtection. Here you can see that it is registered as a singleton. So I don't understand the issue.

Joundill
  • 6,828
  • 12
  • 36
  • 50
Dylan Snel
  • 671
  • 2
  • 7
  • 26

0 Answers0