我们使用 EntityFramework Core 和 Identity Server4 来存储配置数据。我们是否需要自定义实现 IClientStore(即 FindClientByIdAsync)接口(interface)来从数据库获取客户端?
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());
}
}
最佳答案
无需自己动手。 IClientStore 的 Entity Framework Core 实现已存在于 IdentityServer4.EntityFramework 包中。
可以像这样注册:
services.AddIdentityServer()
//.AddInMemoryClients(new List<Client>())
.AddConfigurationStore(options => options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString));
请参阅示例存储库以获取完整的代码示例: https://github.com/IdentityServer/IdentityServer4/tree/main/src/EntityFramework/host
关于identityserver4 - IClientStore 的自定义实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48436423/