我们使用 Entity Framework Core 和 Identityserver4 来存储配置数据。我们是否需要自定义实现 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());
}
}
无需自己动手。 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 https://github.com/IdentityServer/IdentityServer4/tree/main/src/EntityFramework/host
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)