예제 #1
0
        public async Task <UserDTO> Register(UserToRegisterDTO userToRegister)
        {
            try
            {
                //Set username to lowercase
                userToRegister.Username = userToRegister.Username.ToLower();

                //Hash password
                byte[] passwordHash, passwordSalt;
                CreatePasswordHash(userToRegister.Password, out passwordHash, out passwordSalt);

                //Prepare to save user in databse
                var userToCreate = new User
                {
                    Username = userToRegister.Username,
                    Email    = userToRegister.Email
                };
                userToCreate.PasswordHash    = passwordHash;
                userToCreate.PasswordSalt    = passwordSalt;
                userToCreate.CreatedOn       = DateTime.Now;
                userToCreate.VerificationKey = GetUniqueKey(128);
                userToCreate.IsVerified      = false;


                //Create the user
                await _genericRepo.AddAsync(userToCreate);

                //Convert user
                var createdUser = await _userRepo.GetFromUsernameAsync(userToRegister.Username);

                //Create default portfolio
                var defaultPortfolio = new PortfolioToCreateDTO
                {
                    Name = "default",
                };
                await _portfolioService.CreatePortfolioAsync(defaultPortfolio, createdUser.UserId, true);

                var userToReturn = new UserDTO
                {
                    UserId          = createdUser.UserId,
                    Username        = createdUser.Username,
                    VerificationKey = createdUser.VerificationKey,
                    Email           = createdUser.Email
                                      //More fields here
                };

                return(userToReturn);
            }
            catch (Exception ex)
            {
                throw new Exception("UserService failed to register user");
            }
        }
예제 #2
0
        public async Task <IActionResult> Put(PortfolioToCreateDTO portfolio)
        {
            try
            {
                portfolio.Description = _htmlEncoder.Encode(_javaScriptEncoder.Encode(portfolio.Description));
                portfolio.Goal        = _htmlEncoder.Encode(_javaScriptEncoder.Encode(portfolio.Goal));
                portfolio.Name        = _htmlEncoder.Encode(_javaScriptEncoder.Encode(portfolio.Name));

                int userId = Int32.Parse(this.User.FindFirstValue(ClaimTypes.NameIdentifier));
                await _portfolioServices.CreatePortfolioAsync(portfolio, userId, false);

                return(StatusCode(201));
            }
            catch
            {
                return(StatusCode(500, "Something went wrong while attempting to create a new portfolio"));
            }
        }