Custom Implementation of IClientStore

2019-06-08 09:35发布

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());
    }
}

1条回答
太酷不给撩
2楼-- · 2019-06-08 10:04

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:

services.AddIdentityServer()
  //.AddInMemoryClients(new List<Client>())
  .AddConfigurationStore(options => options.ConfigureDbContext = builder => 
      builder.UseSqlServer(connectionString));

See example repository for full code example: https://github.com/IdentityServer/IdentityServer4.EntityFramework/tree/dev/src/Host

查看更多
登录 后发表回答