问题描述
我们正在将EntityFramework Core与Identity Server4一起使用来存储配置数据.我们是否需要自定义实现IClientStore(即FindClientByIdAsync)接口才能从数据库中获取客户端?
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());
}
}
推荐答案
无需自己动手. IdentityServer4.EntityFramework包中已经存在IClientStore的Entity Framework Core实现.
No need to roll your own. An Entity Framework Core implementation of IClientStore already exists in the IdentityServer4.EntityFramework package.
可以这样注册:
services.AddIdentityServer()
//.AddInMemoryClients(new List<Client>())
.AddConfigurationStore(options => options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString));
有关完整的代码示例,请参见示例存储库: https://github.com/IdentityServer/IdentityServer4/tree/main/src/EntityFramework/host
See example repository for full code example:https://github.com/IdentityServer/IdentityServer4/tree/main/src/EntityFramework/host
这篇关于IClientStore的自定义实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!