Exemple #1
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                _logger.Verbose("Invalid {@Input}", ModelState.Values);
                return(Page());
            }
            var context = await _identityServerInteractionService.GetAuthorizationContextAsync(ReturnUrl);

            if (context == null)
            {
                _logger.Verbose("ReturnUrl invalid, cannot find authorization context", ModelState.Values);
                return(BadRequest());
            }

            using var logContext = LogContext.PushProperty("MobileNumber", MobileNumber);
            var existingUsers = await _userManager.FindAllByPhoneNumberAsync(MobileNumber);

            string otpCode;

            if (!existingUsers.Any())
            {
                _logger.Information("User not exists, trying to authenticate for registration", ModelState.Values);
                otpCode = _otpService.GenerateOtp(MobileNumber);
            }
            else
            {
                _logger.Information("User already exists, generating user otp", ModelState.Values);
                otpCode = await _userManager.GenerateChangePhoneNumberTokenAsync(existingUsers[0], MobileNumber);
            }

            await this.SendOtpCode(MobileNumber, otpCode);

            return(RedirectToPage("VerifyOtp"));
        }