Example #1
0
        public IActionResult Register(RegisterDTO user)
        {
            var userExists = _db.users.FirstOrDefault(x =>
                                                      x.email == user.email);

            if (userExists != null)
            {
                return(BadRequest("User Already Exists"));
            }

            byte[] passwordHash;
            byte[] passwordSalt;
            _passwordManager.CreatePasswordHash(user.password, out passwordHash, out passwordSalt);

            User newUser = new User
            {
                email        = user.email,
                passwordHash = passwordHash,
                passwordSalt = passwordSalt,
                name         = user.name,
                nationalId   = user.nationalId
            };

            _db.users.Add(newUser);
            _db.SaveChanges();

            return(Ok(new { message = "User Created" }));
        }
Example #2
0
        public UserViewModel Register(UserViewModel model)
        {
            if (_unitOfWork.AuthRepository.EmailExists(model.Email))
            {
                return(null);
            }

            model.IsActive         = true;
            model.IsEmailConfirmed = false;

            PasswordManager.CreatePasswordHash(model.Password, out byte[] passwordHash, out byte[] passwordSalt);

            User modelMapping = _mapper.Map <User> (model);

            modelMapping.Address = _mapper.Map <Address> (model.AddressViewModel);

            modelMapping.PasswordHash = passwordHash;
            modelMapping.PasswordSalt = passwordSalt;

            modelMapping = _unitOfWork.AuthRepository.Register(modelMapping);
            _unitOfWork.SaveChanges();

            model = _mapper.Map <UserViewModel> (modelMapping);

            return(model);
        }
Example #3
0
        public bool ChangePassword(ChangePasswordModel model)
        {
            PasswordManager.CreatePasswordHash(model.Password, out byte[] passwordHash, out byte[] passwordSalt);

            ChangePasswordDbModel dbModel = new ChangePasswordDbModel
            {
                Email        = model.Email,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt
            };

            if (_unitOfWork.AuthRepository.EmailExists(model.Email))
            {
                _unitOfWork.UserRepository.ChangePassword(dbModel);
                _unitOfWork.SaveChanges();

                return(true);
            }

            return(false);
        }
        public async Task <IActionResult> Register(RegisterViewModel registerViewModel)
        {
            if (ModelState.IsValid)
            {
                UserDTO userFromDB = await _usersService.GetUserByEmailAsync(registerViewModel.Email);

                if (userFromDB == null)
                {
                    string passwordHash, passwordSalt;

                    PasswordManager.CreatePasswordHash(registerViewModel.Password, out passwordHash, out passwordSalt);

                    UserDTO newUserDTO = new UserDTO()
                    {
                        Id           = Guid.NewGuid(),
                        Name         = registerViewModel.Name ?? registerViewModel.Email,
                        Email        = registerViewModel.Email,
                        PasswordHash = passwordHash,
                        PasswordSalt = passwordSalt
                    };

                    CreatedUserRole newUserParams = await _usersService.CreateUserRoleAsync(newUserDTO, DefaultRolesList.User.ToString()); //, RolesList.Admin.ToString()"Admin",

                    await AuthenticateAsync(newUserParams.Email, newUserParams.UserRolesID[0]);
                }
                else
                {
                    ModelState.AddModelError("", "Такой пользователь существует");

                    //return Content("Такой пользователь существует");

                    return(View());
                }

                return(Redirect(registerViewModel.ReturnURL ?? "/Home/Index"));
            }

            return(View());
        }
Example #5
0
        public User Create(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_context.Users.Any(x => x.Username == user.Username))
            {
                throw new AppException("Username \"" + user.Username + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            PasswordManager.CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);
            _context.SaveChanges();
            return(user);
        }
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterModel registerModel) //
        {
            if (ModelState.IsValid)
            {
                UserDTO userFromDB = await _usersService.GetUserByEmailAsync(registerModel.Email);

                if (userFromDB == null)
                {
                    try
                    {
                        string passwordHash, passwordSalt;

                        PasswordManager.CreatePasswordHash(registerModel.Password, out passwordHash, out passwordSalt);

                        UserDTO newUserDTO = new UserDTO()
                        {
                            Id           = Guid.NewGuid(),
                            Name         = registerModel.Name ?? registerModel.Email,
                            Email        = registerModel.Email,
                            PasswordHash = passwordHash,
                            PasswordSalt = passwordSalt
                        };

                        CreatedUserRole newUserParams = await _usersService.CreateUserRoleAsync(newUserDTO, DefaultRolesList.User.ToString()); //, RolesList.Admin.ToString()"Admin",

                        return(Ok(newUserDTO));
                    }
                    catch (Exception ex)
                    {
                        return(BadRequest(new { message = ex.Message }));
                    }
                }
                return(BadRequest(new { message = "Такой пользователь существует" }));
            }
            return(BadRequest(new { message = "Username or password is incorrect" }));
        }
        public void InitializeWithUsersAndRoles()

        {
            Role roleAdmin = _unitOfWork.RoleRepository.FindBy(u => u.Name.Equals(DefaultRolesList.Admin.ToString())).FirstOrDefault();

            Role roleUser = _unitOfWork.RoleRepository.FindBy(u => u.Name.Equals(DefaultRolesList.User.ToString())).FirstOrDefault();


            if (roleAdmin == null)
            {
                roleAdmin = new Role()
                {
                    Id = Guid.NewGuid(), Name = DefaultRolesList.Admin.ToString()
                };

                _unitOfWork.RoleRepository.Add(roleAdmin);
                _unitOfWork.SaveChange();
            }

            if (roleUser == null)
            {
                roleUser = new Role()
                {
                    Id = Guid.NewGuid(), Name = DefaultRolesList.User.ToString()
                };

                _unitOfWork.RoleRepository.Add(roleUser);
                _unitOfWork.SaveChange();
            }

            UserRole adminUserRole = _unitOfWork.UserRoleRepository.FindBy(u => u.RoleId.Equals(roleAdmin.Id)).FirstOrDefault();//

            if (adminUserRole == null)
            {
                string passwordHash, passwordSalt;

                PasswordManager.CreatePasswordHash(_appSettings.InitialAdminPassword, out passwordHash, out passwordSalt);

                User newAdmin = new User()
                {
                    Id           = Guid.NewGuid(),
                    PasswordHash = passwordHash,
                    Name         = _appSettings.InitialAdminName,
                    Email        = _appSettings.InitialAdminEmail,
                    PasswordSalt = passwordSalt
                };

                adminUserRole = new UserRole()
                {
                    Id               = Guid.NewGuid(),
                    UserId           = newAdmin.Id,
                    RoleId           = roleAdmin.Id,
                    RegistrationDate = DateTime.Now,
                };

                _unitOfWork.UserRepository.Add(newAdmin);

                _unitOfWork.UserRoleRepository.Add(adminUserRole);

                _unitOfWork.SaveChange();
            }
        }