identityserver4 - IClientStore 的自定义实现

标签 identityserver4

我们使用 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/

相关文章:

asp.net - 更新我的 identityserver4 后不断收到无效范围错误

javascript - 使用 oidc-client.js 的检查 session 的 CSP 问题

asp.net-mvc - 检测 IdentityServer 用户是否在匿名用户可用的页面上登录

asp.net-core - 表单 POST 上的 OpenIdConnect 重定向

nginx - 通过 IdentityServer4 认证后,NGINX 背后的 .Net Core 返回 502 Bad Gateway

oauth-2.0 - 如果我有服务器端代理,我可以将 Oauth2 授权代码流用于 SPA(React 应用程序)吗?

.net - IdentityServer 4 还是 OpenIddict?

architecture - 将用户数据存储在身份验证服务器或资源服务器中?或两者?

asp.net-web-api - 使用 Asp.Net Core Web API 进行授权

asp.net-core - IdentityServer4如何处理returnUrl