首页 > 解决方案 > 带有自定义 ClientStore 的 IdentityServer

问题描述

我有以下。它在测试环境中工作:

启动.cs:

    public void ConfigureServices(IServiceCollection services) {
      // non-IdentityServer code here
      services.AddIdentityServer(
        options => {

        }
        )
          .AddApiAuthorization<UserEntity, LeagoDbContext>()
          .AddClientStore<MyClientStore>()
          ;
      services.AddAuthentication()
        .AddIdentityServerJwt();
      // more non-IdentityServer code
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
       // non-IdentityServer code here
      app.UseAuthentication();
      app.UseIdentityServer();
      // more non-IdentityServer code
    }

MyClientStore.cs:

public async Task<Client> FindClientByIdAsync(string clientId) {
  switch (clientId) {
    case "client":   // test store
      return await Task.FromResult(MyTestClient.TestClient);
    default:
      // this is the problem. Not sure what to do.
}

如果我不在 Startup.cs 中添加 MyClientStore,则真实环境有效,但测试环境无效。所以我不知何故需要看看真正的客户是什么样子(这样我就可以重建它)。或者,我可以将默认客户端存储注入我的自定义客户端,并在上面 switch 语句的默认情况下调用它。但我不确定如何做任何一个。请注意,默认存储又依赖于 ILogger 和 IConfigurationDbContext。

标签: asp.netauthenticationidentityserver4

解决方案


如果没有找到用户或根据您的自定义条件找到任何用户,您可以返回 null

public async Task<Client> FindClientByIdAsync(string clientId) {
  switch (clientId) {
    case "client":   // test store
      return await Task.FromResult(MyTestClient.TestClient);
    default:
       // you could return null here instead if no user
       return await Task.FromResult(MyTestClient.AnotherClient);
}

推荐阅读