SimpleOtp.Net 1.0.0

dotnet add package SimpleOtp.Net --version 1.0.0
                    
NuGet\Install-Package SimpleOtp.Net -Version 1.0.0
                    
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="SimpleOtp.Net" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleOtp.Net" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleOtp.Net" />
                    
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 SimpleOtp.Net --version 1.0.0
                    
#r "nuget: SimpleOtp.Net, 1.0.0"
                    
#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 SimpleOtp.Net@1.0.0
                    
#: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=SimpleOtp.Net&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleOtp.Net&version=1.0.0
                    
Install as a Cake Tool

TotpService - Usage Documentation

TotpService is a stateless OTP service based on TOTP, with a secret split into two parts: server and client. The client receives the OTP along with the client secret for validation.


1️⃣ Dependency Injection Setup

You can add TotpService to the ASP.NET Core service container using the AddSimpleOtp extension:

using Microsoft.Extensions.DependencyInjection;

public void ConfigureServices(IServiceCollection services)
{
    // Default configuration: OtpSize = 6, TimeStep = 30 seconds
    services.AddSimpleOtp();

    // OR with custom configuration
    var otpConfig = new SimpleOtpConfiguration
    {
        OtpSize = 8,     // OTP length
        TimeStep = 60    // OTP validity duration in seconds
    };
    services.AddSimpleOtp(otpConfig);
}

2️⃣ Configurable Parameters

Parameter Description Default Value
OtpSize Number of digits in the generated OTP 6
TimeStep Duration in seconds the OTP is valid 30

Example of custom values:

var otpConfig = new SimpleOtpConfiguration
{
    OtpSize = 8,   // 8-digit OTP
    TimeStep = 60  // OTP valid for 60 seconds
};
services.AddSimpleOtp(otpConfig);

3️⃣ Using ITotpService in Services or Controllers

With dependency injection, you can inject ITotpService directly:

using Microsoft.AspNetCore.Mvc;

public class AuthController : ControllerBase
{
    private readonly ITotpService _totpService;

    public AuthController(ITotpService totpService)
    {
        _totpService = totpService;
    }

    [HttpGet("generate-otp")]
    public IActionResult GenerateOtp()
    {
        var (otp, secret) = _totpService.GenerateOtp();

        return Ok(new
        {
            Otp = otp,
            Secret = secret
        });
    }

    [HttpPost("validate-otp")]
    public IActionResult ValidateOtp([FromBody] string otp, [FromBody] string secret)
    {
        bool isValid = _totpService.ValidateOtp(otp, secret);
        return Ok(new { IsValid = isValid });
    }
}

4️⃣ Complete Example (Minimal API)

var builder = WebApplication.CreateBuilder(args);

// Add OTP service with custom configuration
builder.Services.AddSimpleOtp(new SimpleOtpConfiguration
{
    OtpSize = 6,
    TimeStep = 30
});

var app = builder.Build();

app.MapGet("/generate-otp", (ITotpService otpService) =>
{
    var (otp, secret) = otpService.GenerateOtp();
    return Results.Ok(new { otp, secret });
});

app.MapPost("/validate-otp", (ITotpService otpService, string otp, string secret) =>
{
    bool isValid = otpService.ValidateOtp(otp, secret);
    return Results.Ok(new { isValid });
});

app.Run();

5️⃣ Example Flow Diagram

+----------------+           +----------------+          +-----------------+
|                |           |                |          |                 |
|  TotpService   |  OTP +    |    Client      |  OTP +   | TotpService     |
|  (Server)      | secret    | receives OTP   | secret   | Validates OTP  |
| generates OTP  | --------> | stores secret  | --------> | recombines     |
| and secret     |           | for later use  |          | secret & checks|
+----------------+           +----------------+          +-----------------+

6️⃣ Key Notes

  • TotpService is stateless; no server-side storage is required.
  • The client must keep the secret for OTP validation.
  • Configurable parameters (OtpSize and TimeStep) allow adjusting security and OTP validity duration.
  • Can be injected into any service or controller via ITotpService.
  • Recommended: store masterSecret securely on the server (e.g., Azure Key Vault, environment variable).
  • TimeStep controls OTP validity. Adjust according to your security requirements.

7️⃣ Quick Usage Summary

  1. Register the service with DI:
services.AddSimpleOtp(new SimpleOtpConfiguration { OtpSize = 6, TimeStep = 30 });
  1. Inject ITotpService in a controller or service.

  2. Generate OTP + secret:

var (otp, secret) = _totpService.GenerateOtp();
  1. Send both to the client.

  2. Validate OTP with secret:

bool isValid = _totpService.ValidateOtp(otp, secret);
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
1.0.0 413 11/17/2025