DEV Community

Angelo Pirola
Angelo Pirola

Posted on

How to secure minimal api microservices with asp.net core identity

The project "MinimalApi.AuthJWTIdentity" demonstrates how to configure authentication and authorization using .NET Identity and JWT tokens shared between two microservices (web APIs).

Here is a detailed summary of the project:

Overview

Purpose: To provide an example of configuring authentication and authorization using .NET Identity and JWT tokens for two microservices.

Scope: The example includes minimal configuration and excludes advanced features like token refresh, role management, and claims. However, these features can be added with additional configuration.
Project Structure

Microservices:

Microservice01: Contains the Program.cs and appsettings.json files for its configuration.

Microservice02: Similar structure to Microservice01 with its own
Program.cs and appsettings.json.

MicroserviceAuth: Manages authentication and contains the main logic for user registration and login.

Main Files:

README.md: Describes the project and its purpose.

Program.cs of Microservice01: Sets up the microservice, adds authentication and authorization, and defines a secure endpoint.

Program.cs of Microservice02: Similar to Microservice01 with its own setup.

Program.cs of MicroserviceAuth: Manages user registration, login, and JWT token generation.

Configuration Files:

appsettings.json of Microservice01: Contains JWT settings and logging configuration.

appsettings.json of Microservice02: Similar to Microservice01 with its own configuration.

appsettings.json of MicroserviceAuth: Contains JWT settings and logging configuration for the authentication service.

Setup and Configuration

Each microservice has its own Program.cs file that sets up the web application, adds authentication and authorization services, and defines endpoints.

The appsettings.json files in each microservice contain the JWT token settings and logging configuration.

The MicroserviceAuth service handles user registration and login, generating JWT tokens for authenticated users.

Key Components

Authentication and Authorization: Implemented using .NET Identity and JWT tokens.

Microservices: Each microservice has its own setup and configuration, sharing the JWT token settings across the services.

Endpoints: Secure endpoints are defined in each microservice, requiring authentication to access.

This project provides a foundational example of using .NET Identity and JWT for securing microservices, with the potential for extending functionality with additional configuration.

GitHub Repository

Comments and/or suggestions are always welcome.

Top comments (0)