A while ago, Microsoft released Identity endpoints it felt like godsend, until I realized that it wasn’t working with JWT tokens, instead, it used a different token type, and couldn’t be customized easily. This lead me to continue using JWT auth for my projects, as I used to. Then found it repetitive and I decided to build a nugget package that would make it easy and fast for you to add JWT authentication to ASP.NET Core to Your ASP.NET Core API. In this article, I’ll walk you through adding and using the package in your projects.

Installation and How to add JWT authentication to ASP.NET Core with “The.Jwt.Auth.Endpoints” nugget package

Open your ASP.net core project, and Just add the nugget package to your project (The.Jwt.Auth.Endpoints). Note, this library doesn’t support cookie auth yet! It works purely for small APIs. and is ideal for projects you’re bootstraping quickly, and you don’t want to spend time writing Auth code.

You could also find the source code of the package on Github here.

Features

  • 🔐 Complete Authentication Flow: Login, registration, email confirmation, password reset
  • 🔄 JWT Token Management: Access tokens with refresh token support
  • 📧 Email Integration: Configurable email confirmation and password reset
  • 🌐 Google Social Auth: Optional Firebase Google authentication

API Endpoints

Once configured, the library provides the following endpoints:

EndpointMethodDescription
/api/auth/registerPOSTUser registration with email confirmation
/api/auth/loginPOSTUser login with email/password
/api/auth/refreshPOSTRefresh JWT access token
/api/auth/confirmEmailGETConfirm user email address
/api/auth/forgotPasswordPOSTInitiate password reset process
/api/auth/resetPasswordPOSTComplete password reset
/api/auth/social/googlePOSTGoogle Firebase authentication (optional)

 

Quick Start

1. Installation

2. Basic Setup

Create Your User Model

Configure Services in Program.cs

3. Required Implementations

To do its job in the background, the package needs services that you’ll just have to plugin to the system. These services will tell the package how to create your users, save and retrieve refresh tokens, and send emails. So, you must provide an implementation for these.

User Factory

Refresh Token Repository

Email Sender

Example Usage

When you add JWT authentication to ASP.NET Core, you will need to test it. Here are the requests you can make to test your API.

Register User

Login

Refresh Token

Forgot Password

Reset Password

Google Social Authentication

To enable Google authentication through Firebase:

1. Configure Firebase

  1. Create a Firebase project in the Firebase Console
  2. Enable Authentication and configure Google as a sign-in provider
  3. Download the Service Account Key JSON file
  4. Place the file in your project root and set its Build Action to Content and Copy Always

2. Configure in Code

3. Usage

Configuration Options

JWT Settings

Main Configuration

Security Features

  • JWT Token Validation: Comprehensive token validation with configurable parameters
  • Refresh Token Rotation: Secure refresh token implementation
  • Email Confirmation: Required email verification for new accounts
  • Password Reset: Secure password reset with time-limited tokens
  • Input Validation: Comprehensive validation using Data Annotations
  • Error Handling: Consistent error responses that don’t leak sensitive information
  • Account Lockout: Configurable account lockout after failed attempts

Testing

The library includes comprehensive integration tests. To run them:

Advanced Usage

Custom User Factory

Implement IIdentityUserFactory<TUser> to control user creation:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

 

 

Follow me on social media and stay updated

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.