Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/2020IP/TwentyTwenty.IdentityServer4.EntityFrameworkCore


https://github.com/2020IP/TwentyTwenty.IdentityServer4.EntityFrameworkCore

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# 20|20 IdentityServer4.EntityFrameworkCore

## Deprecated: This repository is no longer in active development or maintenance. Use the official library instead: [IdentityServer4.EntityFramework.Storage](https://github.com/IdentityServer/IdentityServer4.EntityFramework.Storage)

### Entity Framework Core persistence layer for [IdentityServer v4](https://github.com/IdentityServer/IdentityServer4)
[![NuGet](https://img.shields.io/nuget/v/TwentyTwenty.IdentityServer4.EntityFrameworkCore.svg)](https://www.nuget.org/packages/TwentyTwenty.IdentityServer4.EntityFrameworkCore/)

*CI Nuget Feed*
https://ci.appveyor.com/nuget/twentytwenty-identityserver4-e-eghymilgfl2p

### Usage
The primary key type can be configured for ClientStore and ScopeStore.
To facilitate this, when you Register your contexts make sure you use the correct key.

If you have set it up like this you can use EntityFramework Migrations against a single context to build your DB

```
public class YourDataContext : IClientConfigurationContext, IScopeConfigurationContext, IOperationalContext
{
public YourDataContext(DbContextOptions options)
: base(options)
{ }

public override void OnModelCreating(ModelBuilder modelBuilder)
{
...
modelBuilder.ConfigureClientConfigurationContext();
modelBuilder.ConfigureScopeConfigurationContext();
modelBuilder.ConfigureOperationalContext();
...
base.OnModelCreating(modelBuilder);
}
}
```
In the `Startup.cs`, register your DbContext with Entity Framework as normal
```
public void ConfigureServices(IServiceCollection services)
{
...
services.AddEntityFrameworkSqlServer()
.AddDbContext(o => o
.UseSqlServer(connectionString, b =>
b.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name)))
...
}
```
Register your datacontext(s) with the EFCore Contexts
```
public void ConfigureServices(IServiceCollection services)
{
...
var builder = services.AddIdentityServer(options =>
{
options.RequireSsl = false;
});

builder.ConfigureEntityFramework()
.RegisterOperationalStores()
.RegisterClientStore()
.RegisterScopeStore();
...
}
```

#### Old Usage
The primary key type can be configured for ClientStore and ScopeStore. To facilitate this, subclass the `ClientConfigurationContext` and `ScopeConfigurationContext` with the desired key type.
```
public class ClientConfigurationContext : ClientConfigurationContext
{
public ClientConfigurationContext(DbContextOptions options)
: base(options)
{ }
}

public class ScopeConfigurationContext : ScopeConfigurationContext
{
public ScopeConfigurationContext(DbContextOptions options)
: base(options)
{ }
}
```
In order to enable extensibility of the `OperationalContext`, it must be subclassed. The main reason behind this is EFCore requires the `DbContextOptions` constructor to supply a type, which we cannot set to `OperationalContext` as it will not allow this class to be extensible.
```
public class OperationalContextEx : OperationalContext
{
public OperationalContextEx(DbContextOptions options)
: base(options)
{ }
}
```
In the `Startup.cs`, register your DbContexts with Entity Framework
```
public void ConfigureServices(IServiceCollection services)
{
...
services.AddEntityFrameworkSqlServer()
.AddDbContext(o => o
.UseSqlServer(connectionString, b =>
b.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name)))
.AddDbContext(o => o
.UseSqlServer(connectionString, b =>
b.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name)))
.AddDbContext(o => o
.UseSqlServer(connectionString, b =>
b.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name)));
...
}
```
Register the EFCore Contexts
```
public void ConfigureServices(IServiceCollection services)
{
...
var builder = services.AddIdentityServer(options =>
{
options.RequireSsl = false;
});

builder.ConfigureEntityFramework()
.RegisterOperationalStores()
.RegisterClientStore()
.RegisterScopeStore();
...
}
```
#### Contributing
To get started, [sign the Contributor License Agreement](https://www.clahub.com/agreements/2020IP/TwentyTwenty.IdentityServer4.EntityFrameworkCore).