A general purpose, OTP generation, storage, and validation solution.
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 InMemoryOtpStorage
public 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,
[Expiry] [datetime2] NULL
CONSTRAINT [PK_Otp] PRIMARY KEY CLUSTERED
(
[Id] ASC
)) ON [PRIMARY]
GO
DbContext
ASP.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 SqlServerOtpStorage
public 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(new OtpOptions
{
Length = 6,
Expiry = DateTime.Now.AddMinutes(validityInMinutes)
});
//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);
...
}
Use Remove
method to remove OTP from storage if you no longer need the OTP.
var otpStorage = new InMemoryOtpStorage();
var otpProvider = new Otp(otpStorage);
(string myOtp, string myToken) = otpProvider.GenerateAndStoreOtp();
//Once you're done with the OTP verification and want to clean up
otpStorage.Remove(myToken);
//OR
otpProvider.Remove(myToken);