123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Design;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.Drawing.Text;
- using System.Drawing;
- using NeomtechERP.Auth.Core.Global;
- using NeomtechERP.Auth.Core;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Hosting;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.Email;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Application.Services.Interfaces;
- using MTWorkHR.Core.Entities;
- namespace MTWorkHR.Application.Services
- {
- public class OTPService : IOTPService
- {
- private readonly AppSettingsConfiguration appSettings;
- private readonly IMailSender mailSender;
- private readonly IUnitOfWork unitOfWork;
- private readonly IWebHostEnvironment env;
- public OTPService(AppSettingsConfiguration appSettings, IUnitOfWork unitOfWork, IMailSender mailSender,
- IWebHostEnvironment env)
- {
- this.appSettings = appSettings;
- this.mailSender = mailSender;
- this.unitOfWork = unitOfWork;
- this.env = env;
- }
- public async Task<string> RandomOneTimePassword(string userId)
- {
- string oneTimePassword = default;
- for (var index = 0; index < appSettings.OTPSettings.Length; index++)
- {
- oneTimePassword += $"{new Random().Next(0, 10)}";
- }
- var loginOTP = new LoginOTP()
- {
- OTP = oneTimePassword,
- UserId = userId,
- CreateDate = DateTime.Now,
- ExpireDate = DateTime.Now.AddMinutes(appSettings.OTPSettings.ExpirePeriodInMinutes)
- };
- await unitOfWork.LoginOTP.AddAsync(loginOTP);
- await unitOfWork.CompleteAsync();
- return oneTimePassword;
- }
- public async Task<bool> VerifyOTP(string userId, string oneTimePassword)
- {
- if (appSettings.OTPSettings.AllowZeros )
- {
- var dummyOTP = "";
- for (var index = 0; index < appSettings.OTPSettings.Length; index++)
- {
- dummyOTP += "1";
- }
- return oneTimePassword == "1111";
- }
-
- else
- return await unitOfWork.LoginOTP.VerifyOTP(userId, oneTimePassword);
- }
- //public async Task SentOTPBySMS(string phoneNumber, string oneTimePassword, SMSMethod method)
- //{
- // if (appSettings.OTPSettings.SendSMS)
- // {
- // string message = string.Format(appSettings.OTPSettings.MessageBody, oneTimePassword);
- // var smsModel = new SMSDTO
- // {
- // PhoneNumber = phoneNumber,
- // MessageBody = message,
- // Method = method.ToString(),
- // Module = "Auth",
- // };
- // var smsOutput = await smsProvider.SendSmsBySMSProviderSettings(smsModel);
- // }
- // }
- public async Task SentOTPByMail(string userId, string email, string oneTimePassword)
- {
- if (appSettings.OTPSettings.SendEmail)
- {
- string subject = appSettings.OTPSettings.MessageSubject;
- string message = string.Format(appSettings.OTPSettings.MessageBody , oneTimePassword);
- await mailSender.SendEmail(new EmailMessage
- {
- Subject = subject,
- To = email,
- Body = message,
- url = "",
- userId = userId
- });
- }
- }
- }
- }
|