IdentityServer 4, Create Panel to CRUD Clients

asp.net-core c# entity-framework-core identityserver4

Question

Currently I Have configured Identityserver4 as separated project + My WebAPI and store in DB Credentials in IdentityServer.

Now i have problem how to make CRUD(In my frontend API) to IdentityServer(I want from my API add Clients to IdentityServer)

How to make property?

1
4
11/23/2017 7:37:02 AM

Accepted Answer

From IdentityServer4.EntityFramework and IdentityServer4.EntityFramework.Storage, you have access to IConfigurationDbContext (once you've added the required services in ConfigureServices using e.g. AddConfigurationStore). Because this is registered as part of the Dependency Injection system, you can take a dependency on it in one of your controllers. e.g.:

public class ClientsController : ControllerBase
{
    private readonly IConfigurationDbContext _configurationDbContext;

    public ClientsController(IConfigurationDbContext configurationDbContext)
    {
        _configurationDbContext = configurationDbContext;
    }

    // ...
}

IConfigurationDbContext is an abstraction of a standard DbContext, with the following DbSet<T> properties:

  • Clients
  • IdentityResources
  • ApiResources

It also includes both SaveChanges and SaveChangesAsync - Everything one might expect from a DbContext. Because of all of this, you can CRUD each of these entities just like any other Entity Framework Core driven database.

One final thing to note is that there are both Models (in IdentityServer4.Storage) and Entities (in IdentityServer4.EntityFramework.Storage). There are also a few extension methods for mapping between these (e.g. ClientMappers.ToEntity).

Given all of this, you can create a Model inside of your controller (or perhaps somewhere much better encapsulated than directly there). Here's a basic example for creating a new Client:

var clientModel = new Client
{
    ClientId = "",
    ClientName = "",
    // ...
};

_configurationDbContext.Clients.Add(clientModel.ToEntity());

await _configurationDbContext.SaveChangesAsync();

The Client class here comes from IdentityServer4.Models and is then converted to an Entity using a ToEntity extension method I hinted at above. Working with a Model and converting to an Entity is simpler than trying to manipulate an Entity directly - If you're interested, you can see the mapping that takes place here.

This works in the same way for ApiResources, IdentityResources, etc. Use the source code links I've provided if you want to find out more about those specifically, but the information I've provided here should have you covered.

In order to use IdentityServer4 and IdentityServer4.EntityFramework in your API project, you can just add the two references to your API project. After that, you can configure the DI in the same way (using AddIdentityServer in ConfigureServices), but you don't need to add the middleware (using UseIdentityServer in Configure). You can even just use AddIdentityServer().AddConfigurationStore(...) to set up the relevant services, as you don't need a signing key, etc.

12
2/18/2019 2:51:14 PM

Popular Answer

One way you can do this is by bootstrapping the ID4 Quickstart (tutorial located here): http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html

Other option is to use their quickstart seeds located here to speed this up: https://github.com/IdentityServer/IdentityServer4.Samples

Now if you want to implement restfull login there are constraints around it (i wanted to find out as well) check out this question: IdentityServer 4 Restfull Login/Logout



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow