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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With