После решения моей первоначальной проблемы с добавлением UserManager в API (Создание пользователя с помощью IdentityServer4 из нескольких API), который не инициализирует IdentityServer4 (который, в свою очередь, инициализируется внутри веб-приложения, отвечающего только за регистрацию и вход пользователей в систему), я столкнулся с другой проблемой. Из того же API я хочу также получить клиентов и ресурсы из IConfigurationDbContext IdentityServer4.
Пока я пытаюсь сделать следующее: я добавляю ConfigurationDbContext при запуске API, а затем через ClientsController и ClientsRepository я пытаюсь получить доступ к клиентам, как показано ниже.
Startup.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXX"))
);
services.AddDbContext<ConfigurationDbContext>(options =>
options.UseNpgsql(defaultConnection, db => db.MigrationsAssembly("XXXXXXXX"))
);
services.AddIdentityCore<ApplicationUser>(options => {
options.Password.RequireNonAlphanumeric = false;
});
new IdentityBuilder(typeof(ApplicationUser), typeof(IdentityRole), services)
.AddRoleManager<RoleManager<IdentityRole>>()
.AddSignInManager<SignInManager<ApplicationUser>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
ClientsRepository.cs (в .DataAccess):
private readonly IConfigurationDbContext _context;
public bool AutoSaveChanges { get; set; } = true;
public ClientRepository(IConfigurationDbContext context)
{
_context = context;
}
public Task<Client> GetClientAsync(int id)
{
return _context.Clients
.Include(x => x.AllowedGrantTypes)
.Include(x => x.RedirectUris)
.Include(x => x.PostLogoutRedirectUris)
.Include(x => x.AllowedScopes)
.Include(x => x.ClientSecrets)
.Include(x => x.Claims)
.Include(x => x.IdentityProviderRestrictions)
.Include(x => x.AllowedCorsOrigins)
.Include(x => x.Properties)
.Where(x => x.Id == id)
.SingleOrDefaultAsync();
}
Однако я получаю следующую ошибку:
System.InvalidOperationException: Unable to resolve service for type 'IdentityServer4.EntityFramework.Interfaces.IConfigurationDbContext' while attempting to activate 'XXXXXX.Data.Repositories.ClientRepository'.
Я предполагаю, что он снова должен что-то делать с запуском служб, но я не могу его найти.
Кто-нибудь решал что-то подобное?
Лучший, Мариос.