This is forth post of the series: .NET Microservice with ABP
Posts in the Series
Part 3. Administration Service
Part 4. Identity Service (this post)
Part 8. Identity server and Angular App
Table of contents
- Intro
- Add the shared project as a reference to the host
- Update the connection string
- Update the
IdentityServiceHttpApiHostModule
- Create the
DbContextFactory
in the EntityFrameworkCore project - Update the
IdentityServiceDbContext
- Update the
Tasky.IdentityService.EntityFrameworkCore
project - Migration
Intro
In this post we will see how to configure Identity Service. In the identity service we have already add 2 modules Identity and IdentityServer. we need to configure the Identity service to use these modules.
Add the shared project as a reference to the host
We need to do the same thing we did in the admin service. Add the shared project as a reference and clean up host module.
1<ProjectReference Include="..\..\..\..\shared\Tasky.Shared.Hosting\Tasky.Shared.Hosting.csproj" />
Update the connection string
User ID=postgres;Password=postgres;Host=localhost;Port=5432;Database=TaskyIdentityService;Pooling=false;
IdentityServiceHttpApiHostModule
Update the Update the depends on with the shared hosting module.
1[DependsOn( 2 typeof(TaskyHostingModule), 3 typeof(IdentityServiceApplicationModule), 4 typeof(IdentityServiceEntityFrameworkCoreModule), 5 typeof(IdentityServiceHttpApiModule), 6)]
Remove the things which are configured in the shared project.
DbContextFactory
in the EntityFrameworkCore project
Create the 1using System.IO; 2using Microsoft.EntityFrameworkCore; 3using Microsoft.EntityFrameworkCore.Design; 4using Microsoft.Extensions.Configuration; 5 6namespace Tasky.IdentityService.EntityFrameworkCore; 7 8public class IdentityServiceDbContextFactory : IDesignTimeDbContextFactory<IdentityServiceDbContext> 9{ 10 public IdentityServiceDbContext CreateDbContext(string[] args) 11 { 12 var builder = new DbContextOptionsBuilder<IdentityServiceDbContext>() 13 .UseNpgsql(GetConnectionStringFromConfiguration()); 14 15 return new IdentityServiceDbContext(builder.Options); 16 } 17 18 private static string GetConnectionStringFromConfiguration() 19 { 20 return BuildConfiguration() 21 .GetConnectionString(IdentityServiceDbProperties.ConnectionStringName); 22 } 23 24 private static IConfigurationRoot BuildConfiguration() 25 { 26 var builder = new ConfigurationBuilder() 27 .SetBasePath( 28 Path.Combine( 29 Directory.GetParent(Directory.GetCurrentDirectory())?.Parent!.FullName!, 30 $"host{Path.DirectorySeparatorChar}Tasky.IdentityService.HttpApi.Host" 31 ) 32 ) 33 .AddJsonFile("appsettings.json", false); 34 35 return builder.Build(); 36 } 37}
IdentityServiceDbContext
Update the 1using Microsoft.EntityFrameworkCore; 2using Volo.Abp.Data; 3using Volo.Abp.EntityFrameworkCore; 4using Volo.Abp.Identity; 5using Volo.Abp.Identity.EntityFrameworkCore; 6using Volo.Abp.IdentityServer.ApiResources; 7using Volo.Abp.IdentityServer.ApiScopes; 8using Volo.Abp.IdentityServer.Clients; 9using Volo.Abp.IdentityServer.Devices; 10using Volo.Abp.IdentityServer.EntityFrameworkCore; 11using Volo.Abp.IdentityServer.Grants; 12using Volo.Abp.IdentityServer.IdentityResources; 13 14namespace Tasky.IdentityService.EntityFrameworkCore; 15 16[ConnectionStringName(IdentityServiceDbProperties.ConnectionStringName)] 17public class IdentityServiceDbContext : AbpDbContext<IdentityServiceDbContext>, IIdentityDbContext, 18 IIdentityServerDbContext, IIdentityServiceDbContext 19{ 20 public IdentityServiceDbContext(DbContextOptions<IdentityServiceDbContext> options) 21 : base(options) 22 { 23 } 24 25 public DbSet<IdentityUser> Users { get; set; } 26 public DbSet<IdentityRole> Roles { get; set; } 27 public DbSet<IdentityClaimType> ClaimTypes { get; set; } 28 public DbSet<OrganizationUnit> OrganizationUnits { get; set; } 29 public DbSet<IdentitySecurityLog> SecurityLogs { get; set; } 30 public DbSet<IdentityLinkUser> LinkUsers { get; set; } 31 public DbSet<ApiResource> ApiResources { get; set; } 32 public DbSet<ApiResourceSecret> ApiResourceSecrets { get; set; } 33 public DbSet<ApiResourceClaim> ApiResourceClaims { get; set; } 34 public DbSet<ApiResourceScope> ApiResourceScopes { get; set; } 35 public DbSet<ApiResourceProperty> ApiResourceProperties { get; set; } 36 public DbSet<ApiScope> ApiScopes { get; set; } 37 public DbSet<ApiScopeClaim> ApiScopeClaims { get; set; } 38 public DbSet<ApiScopeProperty> ApiScopeProperties { get; set; } 39 public DbSet<IdentityResource> IdentityResources { get; set; } 40 public DbSet<IdentityResourceClaim> IdentityClaims { get; set; } 41 public DbSet<IdentityResourceProperty> IdentityResourceProperties { get; set; } 42 public DbSet<Client> Clients { get; set; } 43 public DbSet<ClientGrantType> ClientGrantTypes { get; set; } 44 public DbSet<ClientRedirectUri> ClientRedirectUris { get; set; } 45 public DbSet<ClientPostLogoutRedirectUri> ClientPostLogoutRedirectUris { get; set; } 46 public DbSet<ClientScope> ClientScopes { get; set; } 47 public DbSet<ClientSecret> ClientSecrets { get; set; } 48 public DbSet<ClientClaim> ClientClaims { get; set; } 49 public DbSet<ClientIdPRestriction> ClientIdPRestrictions { get; set; } 50 public DbSet<ClientCorsOrigin> ClientCorsOrigins { get; set; } 51 public DbSet<ClientProperty> ClientProperties { get; set; } 52 public DbSet<PersistedGrant> PersistedGrants { get; set; } 53 public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; } 54 55 protected override void OnModelCreating(ModelBuilder builder) 56 { 57 base.OnModelCreating(builder); 58 59 builder.ConfigureIdentityService(); 60 builder.ConfigureIdentity(); 61 builder.ConfigureIdentityServer(); 62 } 63}
Tasky.IdentityService.EntityFrameworkCore
project
Update the Update the nuget packages with ef core packages. we will replace the module db context and configure the DbContext options in this module.
1<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="5.1.4"/> 2<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="5.1.4"/> 3<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1"/>
Update the IdentityServiceEntityFrameworkCoreModule
file.
1using System; 2using Microsoft.Extensions.DependencyInjection; 3using Volo.Abp.EntityFrameworkCore; 4using Volo.Abp.Identity.EntityFrameworkCore; 5using Volo.Abp.IdentityServer.EntityFrameworkCore; 6using Volo.Abp.Modularity; 7 8namespace Tasky.IdentityService.EntityFrameworkCore; 9 10[DependsOn( 11 typeof(IdentityServiceDomainModule), 12 typeof(AbpEntityFrameworkCoreModule), 13 typeof(AbpIdentityEntityFrameworkCoreModule), 14 typeof(AbpIdentityServerEntityFrameworkCoreModule) 15)] 16public class IdentityServiceEntityFrameworkCoreModule : AbpModule 17{ 18 public override void ConfigureServices(ServiceConfigurationContext context) 19 { 20 Configure<AbpDbContextOptions>(options => 21 { 22 options.UseNpgsql(); 23 }); 24 AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); 25 context.Services.AddAbpDbContext<IdentityServiceDbContext>(options => 26 { 27 options.ReplaceDbContext<IIdentityDbContext>(); 28 options.ReplaceDbContext<IIdentityServerDbContext>(); 29 30 options.AddDefaultRepositories(true); 31 }); 32 } 33}
Once this is created delete EntityFrameworkCore
folder can be created.
Migration
To create migrations
dotnet ef migrations add Init
To update database
dotnet ef database update