IClientStore的自定义实现

问题描述 投票:1回答:1

我们使用EntityFramework Core和Identity Server4来存储配置数据。我们是否需要自定义实现IClientStore(即FindClientByIdAsync)接口才能从数据库中获取客户端?

 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());
    }
}
identityserver4
1个回答
2
投票

无需自己动手。实体框架IClientStore的核心实现已存在于IdentityServer4.EntityFramework包中。

这可以这样注册:

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

有关完整代码示例,请参阅示例存储库:https://github.com/IdentityServer/IdentityServer4.EntityFramework/tree/dev/src/Host

© www.soinside.com 2019 - 2024. All rights reserved.