Skip to main content

๐Ÿš€Master User Authentication in .NET 8 Web API Email Confirmation, Password Reset, 2FA & Lockout with JWT & Identity๐Ÿ”

Part 5️⃣ - Ocelot Gateway API๐Ÿ›’ Build .NET 8 eCommerce Microservice ๐Ÿš€with API Gateway, Rate Limiting, Caching & more ๐Ÿงบ

Ocelot Configuration File - ocelot.json

This configuration file sets up routes for an API Gateway using Ocelot. The gateway manages requests for three main services: Authentication, Product, and Order APIs. Here's a detailed description of each section:

Authentication Routes

  • Path: /api/authentication/{everything}
  • Host: localhost:5000
  • Methods: GET, POST, PUT, DELETE
  • Rate Limiting: Allows 1 request per 10 seconds.

Product Routes

  1. Get All Products
    • Path: /api/products
    • Host: localhost:5001
    • Method: GET
    • Caching: 60 seconds with custom header eCommerce-Caching-Control.
  2. Get Product By ID
    • Path: /api/products/{id}
    • Host: localhost:5001
    • Method: GET
    • Caching: 60 seconds with custom header eCommerce-Caching-Control.
  3. Modify Products
    • Path: /api/products/{everything}
    • Host: localhost:5001
    • Methods: POST, PUT, DELETE
    • Authentication: Bearer token required.

Order Routes

  1. Get All Orders
    • Path: /api/orders
    • Host: localhost:5002
    • Method: GET
    • Caching: 20 seconds with custom header eCommerce-Caching-Control.
    • Authentication: Bearer token required.
  2. Get Order By ID
    • Path: /api/orders/{id}
    • Host: localhost:5002
    • Method: GET
    • Caching: 60 seconds with custom header eCommerce-Caching-Control.
    • Authentication: Bearer token required.
  3. Modify Orders
    • Path: /api/orders/{everything}
    • Host: localhost:5002
    • Methods: POST, PUT, DELETE
    • Authentication: Bearer token required.

Global Configuration

  • Base URL: https://localhost:5003
  • Rate Limiting: Custom message for exceeding quota: "You are overwhelming me with multiple requests. Relax small."

This setup ensures that your API Gateway effectively manages and routes requests to your microservices, implementing rate limiting, caching, and authentication where necessary.


Program Registration File - Program.cs 

Configuration and Services Setup

  1. Add Ocelot Configuration File

    • Loads the Ocelot configuration from ocelot.json, ensuring it's mandatory and can be reloaded dynamically on changes.
  2. Add JWT Authentication Scheme

    • Configures JWT-based authentication using the application's configuration settings for secure access control.
  3. Add Ocelot and Cache Manager

    • Integrates Ocelot middleware to handle API Gateway functionalities and configures an in-memory caching mechanism with Cache Manager to improve performance.
  4. Add CORS Policy

    • Configures Cross-Origin Resource Sharing (CORS) to allow any header, method, and origin, enabling the application to handle requests from different domains.

Middleware and Application Pipeline

  1. Custom Middleware

    • Adds a middleware component that attaches a signature to each request, enhancing security or tracking.
  2. HTTPS Redirection

    • Ensures all HTTP requests are redirected to HTTPS, enhancing security by enforcing encrypted communication.
  3. CORS Middleware

    • Enables the configured CORS policy, allowing the application to accept cross-origin requests.
  4. Ocelot Middleware

    • Activates Ocelot to manage request routing, load balancing, and other API Gateway functionalities.

Interception Middleware - AttachSignatureToRequest.cs

  1. AttachSignatureToRequest Middleware

    • Adds a custom middleware that injects a signature into the request headers for enhanced security or tracking. This middleware adds a header Api-Gateway with the value Signed.
  2. HTTPS Redirection

    • Ensures all HTTP requests are redirected to HTTPS, enhancing security by enforcing encrypted communication.
  3. CORS Middleware

    • Enables the configured CORS policy, allowing the application to accept cross-origin requests.
  4. Ocelot Middleware

    • Activates Ocelot to manage request routing, load balancing, and other API Gateway functionalities.

By setting up these configurations and services, the application ensures secure, efficient, and flexible handling of requests, while also supporting dynamic configuration updates and enhanced cross-origin resource sharing.


# Here's a follow-up section to encourage engagement and support for Netcode-Hub:

๐ŸŒŸ Get in touch with Netcode-Hub! ๐Ÿ“ซ

1. GitHub: [Explore Repositories] ๐ŸŒ

2. Twitter: [Stay Updated] ๐Ÿฆ

3. Facebook: [Connect Here]๐Ÿ“˜

4. LinkedIn: [Professional Network]๐Ÿ”—

5. Email: [business.netcodehub@gmail.com] ๐Ÿ“ง

# ☕️ If you've found value in Netcode-Hub's work, consider supporting the channel with a coffee!

1. Buy Me a Coffee: [Support Netcode-Hub] ☕️

Comments

Popular Posts

Complete Employee Management System | .NET 8 Blazor Wasm & Web API - Perform CRUD, Print, PDF etc..

.NET 8 Clean Architecture with Blazor CRUD, JWT & Role Authorization using Identity & Refresh Token๐Ÿ”ฅ

Employee Management System | .NET 8 Blazor Wasm- Profile & real-time data retrieval. Update 1