ZCloakNet 2.0.2

dotnet add package ZCloakNet --version 2.0.2
                    
NuGet\Install-Package ZCloakNet -Version 2.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZCloakNet" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZCloakNet" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="ZCloakNet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZCloakNet --version 2.0.2
                    
#r "nuget: ZCloakNet, 2.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZCloakNet@2.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZCloakNet&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=ZCloakNet&version=2.0.2
                    
Install as a Cake Tool

πŸ” ZCloakNet – Authentication Library for .NET

ZCloakNet is a powerful and lightweight authentication library for .NET 8+ that integrates seamlessly with Keycloak, providing everything you need for user registration, activation, login, token refresh, password recovery, and email notifications β€” all in a few lines of code.


πŸš€ Features

  • βœ… Plug-and-play integration with Keycloak using OAuth2/JWT
  • πŸ“¬ Built-in email templates for account activation, welcome, and password reset
  • 🧩 Extensible activation flow using IActivationStore
  • βš™οΈ Ready-to-use endpoints (/auth/register, /auth/login, /auth/activate, etc.)
  • πŸ“ Minimal configuration and zero boilerplate

πŸ“¦ Installation

Add the NuGet package to your project:

dotnet add package ZCloakNet.Auth

βš™οΈ Configuration

Add the following configuration to your appsettings.json:

"ZCloakNet": {
  "Auth": {
    "BaseUrl": "http://localhost:8080",
    "Realm": "myrealm",
    "AdminClientId": "my-api-client",
    "AdminClientSecret": "super-secret-value-123",
    "PublicClientId": "my-app-client"
  },
  "Email": {
    "Host": "smtp.mailserver.com",
    "Port": 587,
    "Username": "no-reply@myapp.com",
    "Password": "email-password-123",
    "From": "no-reply@myapp.com",
    "EnableSsl": true,
    "Templates": {
      "Activation": {
        "Subject": "Activate your MyApp account",
        "BodyFilePath": "Templates/activation.html"
      },
      "Welcome": {
        "Subject": "Welcome to MyApp",
        "BodyFilePath": "Templates/welcome.html"
      },
      "ForgotPassword": {
        "Subject": "Reset your password",
        "BodyFilePath": "Templates/forgot-password.html"
      }
    }
  }
}

πŸ“Œ Explanation of settings:

Key Description
Auth.BaseUrl URL of the Keycloak server
Auth.Realm Realm name configured in Keycloak
AdminClientId / AdminClientSecret Admin client credentials
PublicClientId Client used by public applications
Email.* SMTP configuration and template paths

πŸ› οΈ Setup in Program.cs

To use the library, simply register it in your Program.cs:

var builder = WebApplication.CreateBuilder(args);

// πŸ”§ Register ZCloakNet
builder.Services.AddZCloakNet(builder.Configuration);

// 🧩 Register your activation store implementation
builder.Services.AddScoped<IActivationStore, UserIdentityService>();

var app = builder.Build();

// βš™οΈ Enable middleware and endpoints
app.UseZCloakNet();
app.MapZCloakNet();

app.Run();

βœ… What each method does:

Method Purpose
AddZCloakNet() Loads configuration and registers internal services
AddScoped<IActivationStore, UserIdentityService>() Injects your custom activation logic
UseZCloakNet() Adds internal middleware (error handling, etc.)
MapZCloakNet() Maps all authentication endpoints automatically

πŸ“„ IActivationStore Interface

The library exposes an interface so you can implement activation logic using your own database and domain model:

public interface IActivationStore
{
    Task SaveAsync(IdentityUser user, CancellationToken ct);
    Task<IdentityUser> FindByActivationCodeAsync(string code, CancellationToken ct);
    Task ActivateAsync(string code, CancellationToken ct);
}

πŸ§‘β€πŸ’» Example Implementation

Here’s a simple implementation of IActivationStore using Entity Framework:

public class UserIdentityService : IActivationStore
{
    private readonly UsersDbContext _context;

    public UserIdentityService(UsersDbContext context)
    {
        _context = context;
    }

    public async Task SaveAsync(IdentityUser user, CancellationToken ct)
    {
        await _context.IdentityUsers.AddAsync(user, ct);
        await _context.SaveChangesAsync(ct);
    }

    public async Task<IdentityUser> FindByActivationCodeAsync(string code, CancellationToken ct)
    {
        return await _context.IdentityUsers
            .AsNoTracking()
            .FirstOrDefaultAsync(u => u.ActivationCode == code, ct);
    }

    public async Task ActivateAsync(string code, CancellationToken ct)
    {
        var user = await _context.IdentityUsers.FirstOrDefaultAsync(u => u.ActivationCode == code, ct);
        if (user is null) return;

        user.IsActive = true;
        user.ActivationCode = $"{user.ActivationCode}-activated-{DateTime.UtcNow:yyyyMMddHHmmss}";
        await _context.SaveChangesAsync(ct);
    }
}

🌐 Example Usage in a Controller

[ApiController]
[Route("api/v1/activation")]
public class ActivationController : ControllerBase
{
    private readonly IActivationStore _activationStore;

    public ActivationController(IActivationStore activationStore)
    {
        _activationStore = activationStore;
    }

    [HttpPost("{code}")]
    public async Task<IActionResult> Activate(string code, CancellationToken ct)
    {
        await _activationStore.ActivateAsync(code, ct);
        return Ok(new { message = "Account successfully activated." });
    }
}

πŸ”Œ Available Endpoints

Once you call app.MapZCloakNet();, the following endpoints are available automatically:

Endpoint Method Description
/auth/register POST Register a new user
/auth/activate/{code} PATCH Activate a user account
/auth/login POST Login with username and password
/auth/refresh POST Refresh access token
/auth/logout DELETE Logout and revoke tokens
/auth/forgot-password POST Send password reset email
/auth/reset-password PATCH Reset password with code

βœ… Quick Start

dotnet add package ZCloakNet.Auth
builder.Services.AddZCloakNet(builder.Configuration);
builder.Services.AddScoped<IActivationStore, UserIdentityService>();

app.UseZCloakNet();
app.MapZCloakNet();

πŸ“ src/
 ┣ πŸ“ ZCloakNet.Auth/       # NuGet library
 ┣ πŸ“ MyApp.API/           # Your API project
 ┣ πŸ“ MyApp.Domain/        # Domain entities and interfaces
 ┣ πŸ“ MyApp.Infrastructure/# EF Core + database logic
 β”— πŸ“ MyApp.Tests/         # Unit tests

🀝 Contributing

Contributions, issues, and feature requests are welcome!
Feel free to open an issue or submit a pull request.


πŸ“ License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.2 218 1/1/2026 2.0.2 is deprecated because it is no longer maintained and has critical bugs.
2.0.1 156 1/1/2026 2.0.1 is deprecated because it is no longer maintained and has critical bugs.
2.0.0 161 1/1/2026 2.0.0 is deprecated because it is no longer maintained and has critical bugs.
1.3.1 479 11/18/2025 1.3.1 is deprecated because it is no longer maintained and has critical bugs.
1.3.0 453 11/18/2025 1.3.0 is deprecated because it is no longer maintained and has critical bugs.
1.2.9 363 11/17/2025 1.2.9 is deprecated because it is no longer maintained and has critical bugs.
1.2.8 245 10/28/2025 1.2.8 is deprecated because it is no longer maintained and has critical bugs.
1.2.7 242 10/28/2025 1.2.7 is deprecated because it is no longer maintained and has critical bugs.
1.2.6 229 10/26/2025 1.2.6 is deprecated because it is no longer maintained and has critical bugs.
1.2.5 158 10/25/2025 1.2.5 is deprecated because it is no longer maintained and has critical bugs.
1.2.4 163 10/25/2025 1.2.4 is deprecated because it is no longer maintained and has critical bugs.
1.2.3 207 10/24/2025 1.2.3 is deprecated because it is no longer maintained and has critical bugs.
1.2.2 230 10/20/2025 1.2.2 is deprecated because it is no longer maintained and has critical bugs.
1.2.1 224 10/16/2025 1.2.1 is deprecated because it is no longer maintained and has critical bugs.
1.2.0 228 10/14/2025 1.2.0 is deprecated because it is no longer maintained and has critical bugs.
1.1.0 236 10/9/2025 1.1.0 is deprecated because it is no longer maintained and has critical bugs.
1.0.10 236 10/6/2025 1.0.10 is deprecated because it is no longer maintained and has critical bugs.
1.0.9 312 10/6/2025 1.0.9 is deprecated because it is no longer maintained and has critical bugs.
1.0.8 310 10/6/2025 1.0.8 is deprecated because it is no longer maintained and has critical bugs.
1.0.7 231 10/6/2025 1.0.7 is deprecated because it is no longer maintained and has critical bugs.
Loading failed