예제 #1
0
        public async Task <IActionResult> PostVerifyPhoneModelAsync()
        {
            await LoadPhoneNumber();

            SMSVerification model = new SMSVerification();

            try
            {
                VerificationResource verification = await VerificationResource.CreateAsync(
                    to : PhoneNumber,
                    channel : "sms",
                    pathServiceSid : _settings.VerificationServiceSID
                    );

                if (verification.Status == "pending")
                {
                    model.PhoneNumber = PhoneNumber;
                    return(View("ConfirmPhone", model));
                }

                ModelState.AddModelError("", $"Votre verification n'est pas traitée, contactez l'admin");
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "There was an error sending the verification code, please contact admin");
            }
            return(View("VerifyPhone", model));
        }
예제 #2
0
        public async Task <IActionResult> ResendCode([FromQuery] string phone)
        {
            try
            {
                SMSVerification model = await _unitOfWork.VerificationRepository.GetDbSet().SingleOrDefaultAsync(v => v.Phone == phone);

                if (model == null)
                {
                    return(BadRequest("Phone is not regestred yet"));
                }
                else
                {
                    long code = StaticTools.GenerateCode();
                    try
                    {
                        string response = await _messageService.SendSMS(phone, code);

                        model.Code = code;
                        _unitOfWork.VerificationRepository.Update(model);
                        _unitOfWork.Commit();
                        return(Ok("Code sent"));
                    }
                    catch (Exception ex)
                    {
                        return(BadRequest(ex.Message));
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(Problem(e.Message));
            }
        }
예제 #3
0
        }// End Class

        public async Task <IActionResult> VerifyPhoneAsync()
        {
            await LoadPhoneNumber();

            SMSVerification model = new SMSVerification();

            model.PhoneNumber = PhoneNumber;
            return(View(model));
        }
예제 #4
0
        public async Task <IActionResult> PostVerifyPhoneCodeAsync(SMSVerification input)
        {
            await LoadPhoneNumber();

            if (!ModelState.IsValid)
            {
                return(View("ConfirmPhone", input));
            }

            try
            {
                VerificationCheckResource verification = await VerificationCheckResource.CreateAsync(
                    to : PhoneNumber,
                    code : input.VerificationCode,
                    pathServiceSid : _settings.VerificationServiceSID
                    );

                if (verification.Status == "approved")
                {
                    var identityUser = await _userManager.GetUserAsync(User);

                    identityUser.PhoneNumberConfirmed = true;
                    var updateResult = await _userManager.UpdateAsync(identityUser);

                    if (updateResult.Succeeded)
                    {
                        ViewBag.urlAppWeb = _configuration["URLAppWeb"];
                        return(View("ConfirmPhoneSuccess"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Erreur lors de la vérification du code, Veuillez reessayer");
                    }
                }
                else
                {
                    ModelState.AddModelError("", $"Erreur lors de la vérification du code: {verification.Status}");
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("",
                                         "Erreur lors de la vérification, vérifiez que votre code est correct et reessayez");
            }

            return(View("ConfirmPhone", input));
        }
예제 #5
0
        public async Task <IActionResult> VerifyCode([FromBody] VerifyCodeRequestModel requestModel)
        {
            try
            {
                SMSVerification model = await _unitOfWork.VerificationRepository.GetDbSet().SingleOrDefaultAsync(v => v.Phone == requestModel.Phone);

                if (model == null)
                {
                    return(NotFound("Phone number is incorrect"));
                }
                else
                {
                    if (model.Code == requestModel.Code)
                    {
                        User user = await _unitOfWork.UserRepository.GetDbSet().SingleOrDefaultAsync(u => u.Phone == model.Phone);

                        if (user == null)
                        {
                            _logger.Log(LogLevel.Error, new Exception("database error"), "cannot get user by phone", model);
                            return(Problem("Database error"));
                        }
                        try
                        {
                            model.IsVerified = true;
                            _unitOfWork.VerificationRepository.Update(model);
                            _unitOfWork.Commit();
                            return(Ok(new TokenResponseModel(requestModel.Phone, _authService.CreateToken(user))));
                        }
                        catch (Exception e)
                        {
                            _unitOfWork.Rollback();
                            _logger.Log(LogLevel.Error, e.Message, e, model);
                            return(Problem(e.Message));
                        }
                    }
                    else
                    {
                        return(BadRequest("Code is incorrect"));
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(Problem(e.Message));
            }
        }
예제 #6
0
        public async Task <IActionResult> PostVerifyPhoneCodeAsync(SMSVerification input)
        {
            await LoadPhoneNumber();

            if (!ModelState.IsValid)
            {
                return(View("ConfirmPhone", input));
            }

            try
            {
                VerificationCheckResource verification = await VerificationCheckResource.CreateAsync(
                    to : PhoneNumber,
                    code : input.VerificationCode,
                    pathServiceSid : _settings.VerificationServiceSID
                    );

                if (verification.Status == "approved")
                {
                    var identityUser = await _userManager.GetUserAsync(User);

                    identityUser.PhoneNumberConfirmed = true;
                    var updateResult = await _userManager.UpdateAsync(identityUser);

                    if (updateResult.Succeeded)
                    {
                        return(View("ConfirmPhoneSuccess"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "There was an error confirming the verification code, please try again");
                    }
                }
                else
                {
                    ModelState.AddModelError("", $"There was an error confirming the verification code: {verification.Status}");
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("",
                                         "There was an error confirming the code, please check the verification code is correct and try again");
            }

            return(View("ConfirmPhone", input));
        }
예제 #7
0
        public async Task <IActionResult> PostVerifyPhoneModelAsync(string number = null, string id = null)
        {
            await LoadPhoneNumber();

            SMSVerification model = new SMSVerification();

            if (number != null)
            {
                this.PhoneNumber = number;
            }

            try
            {
                if (id != null)
                {
                    ApplicationUser user = await _userManager.FindByIdAsync(id);

                    user.PhoneNumber = this.PhoneNumber;
                    _userManager.UpdateAsync(user);
                }

                VerificationResource verification = await VerificationResource.CreateAsync(
                    to : PhoneNumber,
                    channel : "sms",
                    pathServiceSid : _settings.VerificationServiceSID
                    );

                if (verification.Status == "pending")
                {
                    model.PhoneNumber = PhoneNumber;
                    return(View("ConfirmPhone", model));
                }

                ModelState.AddModelError("", $"Your verification is not pending, please constact admin");
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "There was an error sending the verification code, please contact admin");
            }
            return(View("VerifyPhone", model));
        }
예제 #8
0
        private async Task <IActionResult> AdminLoginHandler([FromQuery] string phone)
        {
            try
            {
                if (StaticTools.PhoneValidator(phone))
                {
                    User user = await _unitOfWork.UserRepository.GetDbSet().SingleOrDefaultAsync(u => u.Phone == phone);

                    long   code     = StaticTools.GenerateCode();
                    string response = await _messageService.SendSMS(phone, code);

                    if (user == null)
                    {
                        try
                        {
                            SMSVerification verification = new SMSVerification(code, phone);
                            User            newUser      = new User(phone)
                            {
                                Role = "Admin"
                            };
                            _unitOfWork.UserRepository.Insert(newUser);
                            verification.User = newUser;
                            _unitOfWork.VerificationRepository.Insert(verification);
                            _unitOfWork.Commit();
                            return(Ok("Code sent"));
                        }
                        catch (Exception)
                        {
                            _unitOfWork.Rollback();
                            return(Problem("Database error"));
                        }
                    }
                    else
                    {
                        SMSVerification verification = await _unitOfWork.VerificationRepository.GetDbSet().SingleOrDefaultAsync(v => v.User.Phone == phone);

                        try
                        {
                            verification.Code = code;
                            user.Role         = "Admin";
                            _unitOfWork.VerificationRepository.Update(verification);
                            _unitOfWork.UserRepository.Update(user);
                            _unitOfWork.Commit();
                            return(Ok("Code sent"));
                        }
                        catch (Exception)
                        {
                            _unitOfWork.Rollback();
                            return(Problem("Database error"));
                        }
                    }
                }
                else
                {
                    return(BadRequest(StaticTools.PhoneValidationError));
                }
            }
            catch (Exception e)
            {
                _logger.Log(LogLevel.Error, e, e.Message);
                return(Problem(e.Message));
            }
        }