Example #1
0
        public async Task <bool> SaveOtpAsync(OtpMaster otp)
        {
            bool isSuccess = false;

            // In case from Generating OTP from Automatic redirect to Change Password because of not complex password multiple OTPs can be generated
            // Use this validation to restrict user to generate multiple OTPs
            if (await ValidateUser(otp.UserId, AspectEnums.UserValidationType.LastAttemptDuration))
            {
                _dbContext.Entry(otp).State = EntityState.Added;
                isSuccess = await _dbContext.SaveChangesAsync() > 0;
            }
            return(isSuccess);
        }
        public async Task <string> GetOtpAsync(int userId)
        {
            var    uniqueString = AppUtil.GetUniqueGuidString();
            string otpString    = AppUtil.GetUniqueRandomNumber(100000, 999999); // Generate a Six Digit OTP
            var    otp          = new OtpMaster {
                Guid = uniqueString, Otp = otpString, CreatedDate = DateTime.Now, UserId = userId, Attempts = 0
            };

            if (await _securityRepository.SaveOtpAsync(otp))
            {
                return(uniqueString);
            }

            return(string.Empty);
        }
Example #3
0
        public async Task <OtpMasterViewModel> AddOtp(int UserId, string Email = null, string Mobile = null)
        {
            var checkotp = await GetOtpData(UserId);

            string OtpValue = string.Empty;

            if (checkotp != null)
            {
                UpdateOtp(checkotp.Id);
            }
            OtpValue = _userService.GenerateRandomOTPWithPassword().ToString();
            string alpha = string.Empty; string numeric = string.Empty;

            foreach (char str in OtpValue)
            {
                if (char.IsDigit(str))
                {
                    if (numeric.Length < 6)
                    {
                        numeric += str.ToString();
                    }
                    else
                    {
                        alpha += str.ToString();
                    }
                }
                else
                {
                    alpha += str.ToString();
                }
            }

            int Regtypeid = 0;

            if (!String.IsNullOrEmpty(Email))
            {
                Regtypeid = await _registerTypeService.GetRegisterId(Core.Enums.enRegisterType.Email);
            }
            else if (!String.IsNullOrEmpty(Mobile))
            {
                Regtypeid = await _registerTypeService.GetRegisterId(Core.Enums.enRegisterType.Mobile);
            }

            var currentotp = new OtpMaster
            {
                UserId      = UserId,
                RegTypeId   = Regtypeid,
                OTP         = numeric,
                CreatedTime = DateTime.UtcNow,
                ExpirTime   = DateTime.UtcNow.AddHours(2),
                Status      = 0,
                CreatedDate = DateTime.Now,
                CreatedBy   = UserId
            };

            _customRepository.Add(currentotp);

            if (!String.IsNullOrEmpty(Email))
            {
                SendEmailRequest request = new SendEmailRequest();
                request.Recepient = Email;
                // request.Subject = EnResponseMessage.LoginEmailSubject;
                // request.Body = EnResponseMessage.SendMailBody + numeric;



                IQueryable Result = await _messageConfiguration.GetTemplateConfigurationAsync(Convert.ToInt16(enCommunicationServiceType.Email), Convert.ToInt16(EnTemplateType.LoginWithOTP), 0);

                foreach (TemplateMasterData Provider in Result)
                {
                    Provider.Content = Provider.Content.Replace("###USERNAME###", string.Empty);
                    Provider.Content = Provider.Content.Replace("###Password###", numeric);
                    //string[] splitedarray = Provider.AdditionaInfo.Split(",");
                    //foreach (string s in splitedarray)
                    //{

                    //}
                    request.Body    = Provider.Content;
                    request.Subject = Provider.AdditionalInfo;
                }

                await _mediator.Send(request);
            }
            if (!String.IsNullOrEmpty(Mobile))
            {
                SendSMSRequest request = new SendSMSRequest();
                request.MobileNo = Convert.ToInt64(Mobile);
                request.Message  = EnResponseMessage.SendMailBody + numeric;
                await _mediator.Send(request);
            }

            string _Pass1   = alpha.Substring(0, 20);
            string _Pass11  = _Pass1 + numeric.Substring(0, 3);
            string _Pass2   = alpha.Substring(20, 10);
            string _Pass22  = _Pass2 + numeric.Substring(3, 3);
            string _Pass3   = alpha.Substring(30, 28);
            string password = _Pass11 + _Pass22 + _Pass3;

            OtpMasterViewModel model = new OtpMasterViewModel();

            if (currentotp != null)
            {
                model.UserId      = currentotp.UserId;
                model.RegTypeId   = currentotp.RegTypeId;
                model.OTP         = currentotp.OTP;
                model.CreatedTime = currentotp.CreatedTime;
                model.ExpirTime   = currentotp.ExpirTime;
                model.Status      = currentotp.Status;
                model.Id          = currentotp.Id;
                model.Password    = password;
                model.appkey      = alpha;
                return(model);
            }
            else
            {
                return(null);
            }
        }