Ejemplo n.º 1
0
        public async Task <IActionResult> VerificationPhoneNumber(VerificationPhoneNumberViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user is null)
            {
                return(NotFound(_localizer["UserNotFound", _userManager.GetUserId(User)]));
            }

            if (!TempData.ContainsKey("PhoneVerificationTokenTd"))
            {
                return(NotFound());
            }

            var td      = TempData["PhoneVerificationTokenTd"].ToString() !;
            var tdModel = JsonConvert.DeserializeObject <PhoneTokenTempDataModel>(td);
            var sec     = (int)(tdModel.SendNextSmsTime - DateTime.Now).TotalSeconds;

            if (ModelState.IsValid)
            {
                var result =
                    await _userManager.VerifyChangePhoneNumberTokenAsync(user, model.Token, model.PhoneNumber);

                if (result)
                {
                    user.PhoneNumberConfirmed = true;
                    await _userManager.UpdateAsync(user);

                    TempData.Remove("PhoneVerificationTokenTd");
                    return(RedirectToAction(nameof(Index)));
                }

                model.StatusMessage = sec > 0
                    ? _localizer["WrongPhoneNumberTokenErrorMessage"]
                    : _localizer["ExpirePhoneNumberTokenErrorMessage"];

                TempData.Keep("PhoneVerificationTokenTd");
            }

            model.Interval = sec;
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SendAgainVerificationPhoneNumber(VerificationPhoneNumberViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound(_localizer["UserNotFound", _userManager.GetUserId(User)]));
            }

            var isCanSendNewCode = true;

            if (TempData.ContainsKey("PhoneVerificationTokenTd"))
            {
                var td      = TempData["PhoneVerificationTokenTd"].ToString() !;
                var tdModel = JsonConvert.DeserializeObject <PhoneTokenTempDataModel>(td);
                if (tdModel.SendNextSmsTime >= DateTime.Now)
                {
                    isCanSendNewCode = false;
                }

                TempData.Keep("PhoneVerificationTokenTd");
            }

            if (isCanSendNewCode)
            {
                var code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.PhoneNumber);

                TempData["PhoneVerificationTokenTd"] = JsonConvert.SerializeObject(new PhoneTokenTempDataModel
                {
                    SecretKey       = Guid.NewGuid().ToString(),
                    PhoneNumber     = model.PhoneNumber,
                    SendNextSmsTime = DateTime.Now.AddSeconds(_smsOptions.SendSmsDelay)
                });

                var message = string.Format(_localizer["ConfirmPhoneNumberMessage"], code);
                await _smsSender.SendSmsAsync(model.PhoneNumber, message);
            }

            return(RedirectToAction(nameof(VerificationPhoneNumber)));
        }