IClientStore 的自定义实现

Son*_*ika 2 identityserver4

我们使用 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());
    }
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*ady 5

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

这可以像这样注册:

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

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