DeltaCore.SMSService is a lightweight .NET 8 library that simplifies sending and verifying OTPs via SMS or call using Twilio Verify API, and sending custom messages using Twilio Messaging API.
It also includes a simple throttling mechanism to prevent sending OTPs within 60 seconds to the same number.
✅ Send OTPs via SMS or call
✅ Verify OTPs easily
✅ Send custom text messages
✅ Prevent spamming (resending OTP within 60 seconds)
✅ Built-in error handling and logging
You can install the package from NuGet:
dotnet add package DeltaCore.SMSServiceor via the Package Manager Console:
Install-Package DeltaCore.SMSService- Configure TwilioSettings
In your
appsettings.json, add:
{
"TwilioSettings": {
"AccountSID": "your_account_sid",
"AuthToken": "your_auth_token",
"TwilioPhoneNumber": "your_twilio_phone_number",
"VerifyServiceSID": "your_verify_service_sid"
}
}- Register the Service in
Startup.csorProgram.cs
builder.Services.Configure<TwilioSettings>(builder.Configuration.GetSection("TwilioSettings"));
builder.Services.AddSingleton<ISMSService, TwilioService>();- Using
ISMSServicein Your Application Inject ISMSService where needed, for example in a controller or service:
public class AccountController : ControllerBase
{
private readonly ISMSService _smsService;
public AccountController(ISMSService smsService)
{
_smsService = smsService;
}
[HttpPost("send-otp")]
public async Task<IActionResult> SendOtp(string phoneNumber)
{
var verificationSid = await _smsService.SendOTPAsync(phoneNumber);
if (verificationSid == null)
return BadRequest("OTP was sent recently. Please wait.");
return Ok(new { verificationSid });
}
[HttpPost("verify-otp")]
public async Task<IActionResult> VerifyOtp(string phoneNumber, string code)
{
var isValid = await _smsService.VerifyOTPAsync(phoneNumber, code);
if (!isValid)
return BadRequest("Invalid or expired OTP.");
return Ok("OTP verified successfully.");
}
[HttpPost("send-message")]
public async Task<IActionResult> SendMessage(string phoneNumber, string message)
{
var isSent = await _smsService.SendMessageAsync(phoneNumber, message);
if (!isSent)
return BadRequest("Failed to send message.");
return Ok("Message sent successfully.");
}
}-
OTP requests are rate-limited: the same number cannot request a new OTP within 60 seconds.
-
Errors during sending/verification are logged using ILogger.
-
Supports both SMS and voice calls when sending OTPs by specifying the channel parameter.
This project is licensed under the MIT License.
If you encounter any issues or have questions about using DeltaCore.SMSService, feel free to create an issue on GitHub or reach out to us.
👤 Ahmed Nady