![]() |
VOOZH | about |
dotnet add package MongoDbTokenManager --version 10.1.0
NuGet\Install-Package MongoDbTokenManager -Version 10.1.0
<PackageReference Include="MongoDbTokenManager" Version="10.1.0" />
<PackageVersion Include="MongoDbTokenManager" Version="10.1.0" />Directory.Packages.props
<PackageReference Include="MongoDbTokenManager" />Project file
paket add MongoDbTokenManager --version 10.1.0
#r "nuget: MongoDbTokenManager, 10.1.0"
#:package MongoDbTokenManager@10.1.0
#addin nuget:?package=MongoDbTokenManager&version=10.1.0Install as a Cake Addin
#tool nuget:?package=MongoDbTokenManager&version=10.1.0Install as a Cake Tool
MongoDbTokenManager is an open-source C# class library designed to create, manage, and verify One-Time Tokens (OTTs). These tokens are stored in a MongoDB database, leveraging the MongoDbService package for configuration and access.
TokenIdentifier to ensure type safety for user or resource IDs.Install the NuGet package via the .NET CLI:
dotnet add package MongoDbTokenManager
Configure your MongoDB connection settings in your appsettings.json or environment variables. The library uses MongoDbService which requires the following structure:
{
"MongoDbSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "YourDatabaseName"
}
}
Register the services in your Program.cs or Startup.cs:
using MongoDbTokenManager;
using MongoDbService; // Ensure this is referenced
var builder = WebApplication.CreateBuilder(args);
// Register MongoDbService (required dependency)
builder.Services.AddMongoDbService();
// Register MongoDbTokenManager services
builder.Services.AddMongoDbTokenServices();
var app = builder.Build();
Inject MongoDbTokenService into your class to generate and verify tokens.
using MongoDbTokenManager;
using MongoDbTokenManager.Database;
public class AuthenticationService
{
private readonly MongoDbTokenService _tokenService;
public AuthenticationService(MongoDbTokenService tokenService)
{
_tokenService = tokenService;
}
public async Task<string> SendVerificationCode(string userId)
{
// Generate a 6-digit code valid for 5 minutes (300 seconds)
var token = await _tokenService.Generate(
logId: "UserLogin",
id: new TokenIdentifier(userId),
validityInSeconds: 300,
numberOfDigits: 6
);
// If numberOfDigits is 0 (default), a GUID-based token is generated.
return token; // Send this token via SMS/Email
}
public async Task<bool> VerifyCode(string userId, string code)
{
// Verify the token.
// Returns false if token is expired or does not match.
bool isValid = await _tokenService.Validate(new TokenIdentifier(userId), code);
if (isValid)
{
// Ideally, consume the token so it cannot be used again
await _tokenService.Consume(new TokenIdentifier(userId));
}
return isValid;
}
public async Task<bool> VerifyAndConsume(string userId, string code)
{
// Convenience method to validate and consume in one step
return await _tokenService.ConsumeAndValidate(new TokenIdentifier(userId), code);
}
}
GenerateCreates a new token or updates an existing one if valid.
logId: A string for logging purposes.id: The unique TokenIdentifier for the user/resource.validityInSeconds: How long the token remains valid.numberOfDigits: (Optional) Length of the numeric code. If 0, generates a GUID string.ValidateChecks if a token is valid.
true if the token matches and is not expired.ConsumeDeletes the token associated with the identifier, preventing further use.
ConsumeAndValidateValidates the token and immediately consumes it (deletes it) regardless of the result. Useful for strict one-time-use scenarios.
Expired tokens are automatically cleaned up by MongoDB using a TTL index. By default, tokens are deleted 24 hours after expiry. You can customize this when registering the service:
// Custom cleanup: delete tokens 1 hour after expiry
builder.Services.AddSingleton(sp =>
new MongoDbTokenService(
sp.GetRequiredService<MongoService>(),
cleanupAfterExpiry: TimeSpan.FromHours(1)
));
Set to TimeSpan.Zero to delete tokens immediately upon expiry.
We welcome contributions! If you find a bug or have an idea for improvement, please submit an issue or a pull request on GitHub.
This project is licensed under the GNU GENERAL PUBLIC LICENSE.
Happy coding! 🚀
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on MongoDbTokenManager:
| Package | Downloads |
|---|---|
|
EmailSwitch
Package Description |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.1.0 | 453 | 12/10/2025 |
| 10.0.0 | 253 | 11/24/2025 |
| 7.0.0 | 205 | 11/23/2025 |
| 6.0.0 | 183 | 11/23/2025 |
| 5.1.0 | 261 | 6/28/2025 |
| 5.0.1 | 334 | 3/16/2025 |
| 5.0.0 | 375 | 1/8/2025 |
| 4.0.0 | 395 | 7/28/2024 |
| 3.1.0 | 204 | 7/15/2024 |
| 3.0.1 | 222 | 7/7/2024 |
| 3.0.0 | 174 | 7/7/2024 |
| 2.1.0 | 167 | 7/5/2024 |
| 2.0.0 | 178 | 6/23/2024 |
| 1.0.0 | 173 | 6/23/2024 |