我们正在使用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());
}
}
答案 0 :(得分:2)
无需自己动手。身份框架IClientStore的核心实现已经存在于IdentityServer4.EntityFramework包中。
这可以这样注册:
services.AddIdentityServer()
//.AddInMemoryClients(new List<Client>())
.AddConfigurationStore(options => options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString));
有关完整代码示例,请参阅示例存储库:Link