Exemple #1
0
        public async Task <IActionResult> Register([FromBody] RegisterModel registerModel)
        {
            if (!ModelState.IsValid)
            {
                return(Ok(ServiceResult.Error(ModelState)));
            }

            // create a captcha instance to be used for the captcha validation
            SimpleCaptcha yourFirstCaptcha = new SimpleCaptcha();
            // execute the captcha validation
            bool isHuman = yourFirstCaptcha.Validate(registerModel.UserEnteredCaptchaCode, registerModel.CaptchaId);

            if (isHuman == false)
            {
                return(Ok(ServiceResult.Error("کد امنیتی اشتباه است")));
            }

            // validation unique pjoneNumber and nationalCode
            if (userManager.Users.Any(c => c.NationalCode.Equals(registerModel.NationalCode)))
            {
                return(Ok(ServiceResult.Error("کد ملی متعلق به شخص دیگری است")));
            }

            if (_userPhoneNumberRepository
                .AsQueryable()
                .Any(c => c.PhoneNumber.Equals(registerModel.PhoneNumber)))
            {
                return(Ok(ServiceResult.Error("شماره همراه متعلق به شخص دیگری است")));
            }


            var result = await userManager.CreateAsync(new AppIdentityUser
            {
                NationalCode = registerModel.NationalCode,
                UserName     = registerModel.UserName,
                IsAdmin      = false
            }, registerModel.Password);


            if (result.Succeeded)
            {
                var appUser = await userManager.FindByNameAsync(registerModel.UserName);

                var activationCode = new Random().Next(1000, 9999);

                var verificationTime = _settingRepository.AsQueryable()
                                       .FirstOrDefault(c => c.Key.Equals(Enums.Setting.VerificationTime.ToString()));

                int verificationTimeMin = 3;

                if (verificationTime != null)
                {
                    verificationTimeMin = Convert.ToInt32(verificationTime.Value);
                }

                _userPhoneNumberRepository.Create(new UserPhoneNumber
                {
                    IsConfirm                = false,
                    IsPrimary                = true,
                    PhoneNumber              = registerModel.PhoneNumber,
                    UserId                   = appUser.Id,
                    ActivationCode           = activationCode,
                    ActivationCodeExpireDate = DateTime.Now.AddMinutes(verificationTimeMin)
                });

                new SmsProvider.SmsService().Send(registerModel.PhoneNumber, $"کد فعالسازی شما : {activationCode}");

                return(Ok(ServiceResult <int> .Okay(verificationTimeMin)));
            }

            var errors = result.Errors.Select(c => c.Description).ToList();

            return(Ok(ServiceResult.Error(errors)));
        }