.NET Core Identity Server dynamic Client app registration and authentication

Viewed 1591

I have created an Identity Server using .NET Core and IdentityServer4, I have set of APIs and all calls to these APIs must be authenticated but these APIs might be used by third-party applications so clients can be dynamic Till now example I am finding is set Clients on startup statically like

public void ConfigureServices(IServiceCollection services)
{
   services.AddIdentityServer()
   .AddDeveloperSigningCredential()
   .AddInMemoryApiResources(Config.GetApiResources())
   .AddInMemoryClients(Config.GetClients());
}

public class Config
{
   public static IEnumerable<ApiResource> GetApiResources()
   {
       return new List<ApiResource>
       {
           new ApiResource("resourceApi1", "API Application1")
           new ApiResource("resourceApi2", "API Application2")
       };
   }

   public static IEnumerable<Client> GetClients()
   {
       return new List<Client>
       {
           new Client
           {
               ClientId = "clientApp1",

               // no interactive user, use the clientid/secret for authentication
               AllowedGrantTypes = GrantTypes.ClientCredentials,

               // secret for authentication
               ClientSecrets =
               {
                   new Secret("secret".Sha256())
               },

               AllowedScopes = { "resourceApi1" }
           }
       };
   }
}

Is there a way in IdentityServer implementation to register Client Apps and set dynamically For Example, If I have APIs 1. resourceApi1 2. resourceApi2

Each third-party APIs should be able to register and we should be able to generate ClientID and Client secret for each with what resources they can access and Identity Server authenticates that ClientID and Client Secret?

2 Answers

There is an option to use a client store. By default identity server uses in memory store for finding clients:

services.AddIdentityServer()
    .AddInMemoryClients(Clients)

You can change this and register your own client store

services.AddIdentityServer()
    .AddClientStore<MyClientStore>()

and implement the service

class MyClientStore : IClientStore
{
    public Task<Client> FindClientByIdAsync(string clientId)
    {

    }
}

This would solve dynamic lookup of clients. For registration of clients and their management, you would need to implement your own infrastructure.

First of all configure your IdentityServer using EntityFramework, Then you need to some Apis to add Client and ApiResources.

IdentityServer has its own implementation of Stores using EntityFramework.

You can add new Api for this purpose.

[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class DynamicController : ControllerBase
{
    private readonly ConfigurationDbContext context;

    public DynamicController(ConfigurationDbContext context)
    {
        this.context = context;
    }

    [HttpPost]
    public async Task<ActionResult> AddApiResource(ApiResource apiResource)
    {
        context.ApiResources.Add(apiResource);
        await context.SaveChangesAsync();

        return Ok();
    }

    [HttpPost]
    public async Task<ActionResult> AddClient(Client client)
    {
        context.Clients.Add(client);
        await context.SaveChangesAsync();

        return Ok();
    }
}
Related