Anto Subash.

Published on

IdentityServer and Angular - .NET Microservice with ABP - Part 8

This is eight post of the series: .NET Microservice with ABP

Posts in the Series

Part 1. Initial Setup

Part 2. Shared Project

Part 3. Administration Service

Part 4. Identity Service

Part 5. SaaS Service

Part 6. DB Migration

Part 7. Yarp and Tye

Part 8. Identity server and Angular App (this post)

Part 9. Distributed event bus

Part 10. Docker and CI/CD

Part 11. Add a New service

Part 12. Central Logging

Table of contents

Intro

We have copied the IdentityServer and Angular app from the default tired app. So they are configured to work with the tired app. we will modify the Identity server to use the newly created services and the change the config of the angular app to use the Reverse proxy we created with Yarp.

IdentityServer

Even thought we already have the Identity service. it only provides the API endpoints to manage the user. We need the Identity server to do the JWT auth and also for the login and registration. IdentityServer is the default way to login user in the ABP framework right now. But there are plans to replace IdentityServer and replace it with the OpenIdDict if you want to know more about this go and check here https://github.com/abpframework/abp/issues/11989

Remove the wrong reference

During the code generation by default Identity server will have the reference for the EFCore project. But after moving the Identity server this reference will be wrong. So the first step is to remove all the project reference in the csproj file of the identity server.

Add new reference

Once the old references are removed we need to add the new reference to the identity server project. The Identity server should know about all 3 base services identity, administration and saas. So we need to add the reference for the EFCore projects of all the 3 services. Along with we also need to add the reference for the Shared project so that we can reuse some code.

1<ItemGroup>
2    <ProjectReference Include="..\..\services\administration\src\Tasky.AdministrationService.EntityFrameworkCore\Tasky.AdministrationService.EntityFrameworkCore.csproj" />
3    <ProjectReference Include="..\..\services\identity\src\Tasky.IdentityService.EntityFrameworkCore\Tasky.IdentityService.EntityFrameworkCore.csproj" />
4    <ProjectReference Include="..\..\services\saas\src\Tasky.SaaSService.EntityFrameworkCore\Tasky.SaaSService.EntityFrameworkCore.csproj" />
5    <ProjectReference Include="..\..\shared\Tasky.Shared.Hosting\Tasky.Shared.Hosting.csproj" />
6</ItemGroup>

Update the connection string

Once we added the reference to the EFCore projects we need to add the connection string for the services in the appsettings.json file so that the Identity server can find the database.

1"ConnectionStrings": {
2  "SaaSService": "User ID=postgres;Password=postgres;Host=localhost;Port=5432;Database=TaskySaaSService;Pooling=false;",
3  "IdentityService": "User ID=postgres;Password=postgres;Host=localhost;Port=5432;Database=TaskyIdentityService;Pooling=false;",
4  "AdministrationService": "User ID=postgres;Password=postgres;Host=localhost;Port=5432;Database=TaskyAdministrationService;Pooling=false;"
5},

Update CORS urls

All the services and apps need to contact the Identity server to validate the tokens. so we should add the urls of the service in the CorsOrigins section of the appsettings.json

1"CorsOrigins": "http://localhost:4200,https://localhost:7001,https://localhost:7002,https://localhost:7003",

Update the Authority URL

All the service has Authority url in the appsettings.json file. we have to change that file to point to the identity server.

1"Authority": "https://localhost:7000/",

Update the module dependency

Identity server project should have a IdentityServerModule in the root of the project directory. In that module we need update the module dependency so that the EFCore modules will be loaded correctly. We also should add the shared module.

1typeof(AbpAccountWebIdentityServerModule),
2typeof(AbpAccountApplicationModule),
3typeof(AbpAccountHttpApiModule),
4typeof(AbpAspNetCoreMvcUiBasicThemeModule),
5typeof(AdministrationServiceEntityFrameworkCoreModule),
6typeof(SaaSServiceEntityFrameworkCoreModule),
7typeof(IdentityServiceEntityFrameworkCoreModule),
8typeof(TaskyHostingModule)

Once this is done. our identity server is ready for our use. launch the identity server and see if you can login as a super admin.

Running Identity server

1dotnet run

This should launch the identity server and it should be running in the port 7000

Angular App

angular app in abp is a self contained app. there is not dependency to any other abp project. so to run the we just have to point the angular app in the correct direction and it should work. We need to change 2 things in the angular app. one is the OAuthConfig so that it know where is the identity server is running and the API endpoint which is the reverse proxy in our case which has all the endpoints. you will be able to find the environment.ts file in apps\angular\src\environments.

1import { Environment } from '@abp/ng.core';
2
3const baseUrl = 'http://localhost:4200';
4
5export const environment = {
6  production: false,
7  application: {
8    baseUrl: 'http://localhost:4200/',
9    name: 'Tasky',
10    logoUrl: '',
11  },
12  oAuthConfig: {
13    issuer: 'https://localhost:7000',
14    redirectUri: baseUrl,
15    clientId: 'Tasky_App',
16    responseType: 'code',
17    scope: 'offline_access IdentityService AdministrationService SaaSService role email openid profile',
18    requireHttps: true
19  },
20  apis: {
21    default: {
22      url: 'https://localhost:7500',
23      rootNamespace: 'Tasky',
24    }
25  },
26} as Environment;

Run

Once you have updated the environment.ts make sure the packages are installed for your angular app.

To install packages

1yarn

you need to have yarn installed for this command to work

This command will install all the packages needed for your angular app.

To Run

To run the angular app run the following command

1yarn start

This will start the angular dev server and your application will be running in the port 4200.