OnlyOTP - An OTP generation, storage, and validation solution

Manager — SDE
Search for a command to run...

Manager — SDE
No comments yet. Be the first to comment.
← Previous: What’s New in the SDK and Tooling for .NET 9 .NET Aspire is a set of tools, templates, and packages designed to help developers build observable, production-ready apps that embrace cloud-native and distributed computing best practices. .N...

← Previous: What’s New in .NET Libraries for .NET 9 The .NET 9 SDK introduces several new capabilities and improvements to streamline development workflows, enhance build and test experiences, improve NuGet security and performance, and provide great...

← Previous: What’s New in .NET MAUI for .NET 9 .NET 9 adds a wide range of enhancements and new APIs across the base class libraries. These improvements touch everything from low-level data structures and cryptography to JSON serialization, text pr...

← Previous: What’s New in ML.NET .NET Multi-platform App UI (.NET MAUI) in .NET 9 focuses on improving overall product quality, test coverage, performance, and stability. While previous releases introduced major new capabilities and controls, the emp...

← Previous: What’s new in ASP.NET Core 9.0 ML.NET continues to evolve as a full-featured, open-source machine learning framework for .NET developers, enabling you to create, train, and deploy custom ML models directly in your .NET applications. ML....


Install Nuget Package https://www.nuget.org/packages/OnlyOTP
var otpProvider = new Otp();
//Generates 6 digit OTP by default
var otp = otpProvider.GenerateOtp();
var otpWithSpecifiedLength = otpProvider.GenerateOtp(new OtpOptions { Length = 10 });
Install additional Nuget Package https://www.nuget.org/packages/OnlyOtp.Storage.InMemory
Otp with InMemoryOtpStoragepublic void SomeMethod()
{
// Pass InMemoryOtpStorage to Otp Provider.
var otpProvider = new Otp(new InMemoryOtpStorage());
// Returns OTP and OTP Verification Token
// Save this token to retreive generated OTP later
(string myOtp, string myToken) = otpProvider.GenerateAndStoreOtp();
}
public void SomeOtherMethod(string otpEnteredByUser)
{
// Match the stored OTP with OTP entered by user
var isMatched = otpProvider.IsOtpMached(otpEnteredByUser, myToken);
}
Install additional Nuget Package https://www.nuget.org/packages/OnlyOtp.Storage.SqlServer
SqlServerOtpStorage uses an Sql Server to store OTPs. Use this script to generate necessary table and schema.
GO
CREATE SCHEMA [OnlyOtp]
GO
CREATE TABLE [OnlyOtp].[Otp](
[Id] [nvarchar](100) NOT NULL,
[Value] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_Otp] PRIMARY KEY CLUSTERED
(
[Id] ASC
)) ON [PRIMARY]
GO
DbContextASP.NET Core applicationIf you're using ASP.NET Core, you can use Dependency Injection to set-up DbContext used by OnlyOtp.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<OnlyOtpContext>(options =>
{
options.UseSqlServer(@"Data Source=DATABASE_SERVER_NAME;Initial Catalog=DATABASE_NAME;Integrated Security=True;");
});
...
}
And in Controller or any other class, inject this DbContext:
public class HelloController : Controller
{
private readonly OnlyOtpContext _onlyOtpContext;
public HelloController(OnlyOtpContext onlyOtpContext)
{
_onlyOtpContext = onlyOtpContext;
}
}
You can instantiate the DbContext manually.
var options = new DbContextOptionsBuilder<OnlyOtpContext>()
.UseSqlServer(@"Data Source=DATABASE_SERVER_NAME;Initial Catalog=DATABASE_NAME;Integrated Security=True;")
.Options;
var dbContext = new OnlyOtpContext(options);
Otp with SqlServerOtpStoragepublic void SomeMethod()
{
// Pass SqlServerOtpStorage to Otp Provider.
var otpProvider = new Otp(new SqlServerOtpStorage(_onlyOtpContext));
// Get OTP token and generated OTP
(string otp, string token) = otpProvider.GenerateAndStoreOtp();
//Save this token to retreive generated OTP later
...
}
public void SomeOtherActionMethod(string otpEnteredByUser)
{
...
// Match the stored OTP with OTP entered by user
bool isOtpMatched = otpProvider.IsOtpMached(otpEnteredByUser, token);
...
}