public async Task <IActionResult> Register(string username, string password, string phonenumber,
                                                   [FromServices] ISmsSender smsSender, [FromServices] QbDbContext db)
        {
            var pars = new[] { username, password, phonenumber };

            if (pars.Any(string.IsNullOrWhiteSpace))
            {
                return(Res.JsonErrorResult("missing_field", 400,
                                           "One or more of required fields missing or empty: username, password, phonenumber."));
            }

            var exists = null != await _userManager.FindByNameAsync(username);

            if (exists)
            {
                return(Res.JsonErrorResult("exists", 400, "Username already exists."));
            }

            var user = new QbUser
            {
                UserName             = username,
                PhoneNumberConfirmed = false
            };

            var res = await _userManager.CreateAsync(user, password);

            if (res.Succeeded)
            {
                user = await _userManager.FindByNameAsync(username);

                // Creates a full user with all edit abilities for own information.
                var userGuid = new Guid(user.Id);

                // Create a Person with the same guid as the identity user.
                await CreatePerson(userGuid, db);

                var phoneToken = await _userManager.GenerateChangePhoneNumberTokenAsync(user, phonenumber);

                // Ignore used for manual admin verified user.
                if (phonenumber.ToLowerInvariant() != "ignore")
                {
                    if (!await smsSender.SendSms($"QB sign-up code:{phoneToken}", phonenumber))
                    {
                        return
                            (Res.JsonErrorResult("invalid_phone", 400,
                                                 "User created but sms failed, try re-requesting code by changing phonenumber."));
                    }
                }

                return(Res.PlainUtf8($"User {username} successfully created, needs verification."));
            }

            return(Res.JsonErrorResult("identity_error", 400, res.PrettyErrors()));
        }
Exemple #2
0
        public async Task <IActionResult> AddPhoneNumber(AddPhoneNumberViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            // Generate the token and send it
            var user = await GetCurrentUserAsync();

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

            await _smsSender.SendSmsAsync(model.PhoneNumber, "Your security code is: " + code);

            return(RedirectToAction(nameof(VerifyPhoneNumber), new { PhoneNumber = model.PhoneNumber }));
        }