We are using EntityFramework Core with Identity Server4 to store configuration data. Do we need custom implementation of IClientStore(i.e FindClientByIdAsync) interface to fetch client from database?
public class CustomClientStore : IClientStore
{
const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.Quickstart.EntityFramework-2.0.0;trusted_connection=yes;";
public Task<Client> FindClientByIdAsync(string clientId)
{
var options = new DbContextOptionsBuilder<ConfigurationDbContext>();
options.UseSqlServer(connectionString);
var _context = new ConfigurationDbContext(options.Options, new ConfigurationStoreOptions());
var result = _context.Clients.Where(x => x.ClientId == clientId).FirstOrDefault();
return Task.FromResult(result.ToModel());
}
}
No need to roll your own. An Entity Framework Core implementation of IClientStore already exists in the IdentityServer4.EntityFramework package.
This can be registered like so:
See example repository for full code example: https://github.com/IdentityServer/IdentityServer4.EntityFramework/tree/dev/src/Host