示例#1
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    //_logger.LogInformation(3, "User created a new account with password.");
                    return(Ok());
                }
                else
                {
                    return(BadRequest(result.Errors));
                }
            }
            else
            {
                return(BadRequest("Model is not valid"));
            }
        }
示例#2
0
        public async Task <IActionResult> Signup([FromBody] SignupViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (var dbTransaction = _applicationDbContext.Database.BeginTransaction())
                    {
                        var user = new ApplicationUser()
                        {
                            UserName = model.Username
                        };

                        var result = await _userManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            dbTransaction.Commit();

                            return(Ok(user.ToViewModel()));
                        }
                        else
                        {
                            dbTransaction.Rollback();

                            return(BadRequest(new ErrorViewModel()
                            {
                                Error = ErrorCode.ModelError, ErrorDescription = result.Errors.FirstOrDefault().Description
                            }));
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);

                    return(BadRequest(new ErrorViewModel {
                        Error = ErrorCode.ServerError, ErrorDescription = ex.Message
                    }));
                }
            }

            return(BadRequest(new ErrorViewModel()
            {
                Error = ErrorCode.ModelError, ErrorDescription = ModelState.FirstOrDefault().Value.Errors.FirstOrDefault().ErrorMessage
            }));
        }
        public async Task <IdentityResult> Register(SnazzleUserDto dto)
        {
            var user = new SnazzleUser {
                UserName = dto.Email, Email = dto.Email, FirstName = dto.FirstName, LastName = dto.LastName
            };
            var result = await _userManager.CreateAsync(user, dto.Password);

            return(result);
        }
        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()));
        }