Identityserver4 IClientStore的自定义实现
我们使用EntityFramework Core和Identity Server4来存储配置数据。我们是否需要IClientStore(即FindClientByIdAsync)接口的自定义实现来从数据库获取客户端Identityserver4 IClientStore的自定义实现,identityserver4,Identityserver4,我们使用EntityFramework Core和Identity Server4来存储配置数据。我们是否需要IClientStore(即FindClientByIdAsync)接口的自定义实现来从数据库获取客户端 public class CustomClientStore : IClientStore { const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentitySer
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());
}
}
公共类CustomClientStore:IClientStore
{
const string connectionString=@“数据源=(LocalDb)\MSSQLLocalDB;数据库=IdentityServer4.Quickstart.EntityFramework-2.0.0;受信任的_连接=是;”;
公共任务findclientbydasync(字符串clientId)
{
var options=new DbContextOptionsBuilder();
使用SQLServer(connectionString);
var_context=new ConfigurationDbContext(options.options,new ConfigurationStoreOptions());
var result=_context.Clients.Where(x=>x.ClientId==ClientId.FirstOrDefault();
返回Task.FromResult(result.ToModel());
}
}
无需自己动手。IdentityServer4.EntityFramework包中已存在IClientStore的实体框架核心实现
可以这样注册:
services.AddIdentityServer()
//.AddInMemoryClients(new List<Client>())
.AddConfigurationStore(options => options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString));
services.AddIdentityServer()
//.AddInMemoryClients(新列表())
.AddConfigurationStore(选项=>options.ConfigureDbContext=builder=>
UseSqlServer(connectionString));
有关完整代码示例,请参见示例存储库:
好的,谢谢Scott,如果我们必须动态添加客户端,那么我们是否应该为此创建一个端点?动态客户端注册有一个规范,不幸的是IdentityServer没有实现它。因此,是的,您将需要一个新端点。是否有我所不知道的在线样本做同样的事情?(即动态客户端注册)。如果你找到了,请告诉我!默认的客户端存储很弱。它只允许通过在上下文上执行firstordefault来实现findbyid。我看不出重点。