internal async Task <RegisterResponse> SaveUserAccountAsync(ApplicationUser applicationUser, ApplicationUser currentUser, RegisterViewModel viewModel)
        {
            var response = new RegisterResponse();

            if (((currentUser == null || currentUser is Client || currentUser is Driver) && (applicationUser is SystemAdmin || applicationUser is Manager)) ||
                (currentUser is Manager && applicationUser is SystemAdmin))
            {
                response.AddError(_stringLocalizer[CustomStringLocalizer.NO_RIGHTS_TO_ADD_OR_UPDATE_USER]);
                return(response);
            }

            try
            {
                var creationResult = await _userManager.CreateAsync(applicationUser, viewModel.Password);

                if (creationResult.Succeeded)
                {
                    // save user's claims
                    await AddUserClaimsAsync(applicationUser, viewModel);

                    response.UserName = viewModel.Email;
                }
                else
                {
                    response.AddError(_stringLocalizer[CustomStringLocalizer.USER_NOT_CREATED]);
                }
            }
            catch (Exception ex)
            {
                response.AddException(ex);
            }
            return(response);
        }
Пример #2
0
        public RegisterResponse Register(RegisterRequest request)
        {
            RegisterResponse response = new RegisterResponse();

            //check if email is already in use
            if (dbContext.Authentication.Any(user => user.Email == request.Email))
            {
                response.Success = false;
                response.AddError("Email", "Email already exists");
            }
            else
            {
                //create user
                User newUser = new User()
                {
                    Verified = false, Role = "User"
                };

                //create profile
                Profile newProfile = new Profile()
                {
                    FirstName = request.FirstName, LastName = request.LastName, DateCreated = DateTime.Now, DateModified = DateTime.Now
                };
                newUser.Profile = newProfile;

                Authentication newAuth = new Authentication()
                {
                    Email = request.Email, Password = PasswordHelper.HashPassword(request.Password), Type = 0
                };
                newUser.Authentication = newAuth;

                dbContext.Users.Add(newUser);
                dbContext.SaveChanges();

                //create email confirmation token
                string token = TokenHelper.GenerateEmailConfirmToken(newUser);

                //send link to users email
                //localhost/Auth/ConfirmEmail
                //http://localhost:3000/register

                String body = "Email confirmation link: <a href='http://localhost:3000/emailConfirm?token=" + token + "'>Click here</a>";

                EmailHelper.Send(newUser.Authentication.Email, "Email Confirmation", body);

                response.Success = true;
                response.Message = "Successfully registered";
            }

            return(response);
        }