public async Task <ResultResponse <LoggedUserDto> > RegisterAsync(RegisterRequest request) { var user = await _userRepository.GetByEmailAsync(request.Email); if (user != null) { return(new ResultResponse <LoggedUserDto>("User with this email is already exist")); } var passwordSalt = _encryptionService.CreateSalt(); var passwordHash = _encryptionService.CreateHash(request.Password, passwordSalt); user = new User { FirstName = request.FirstName, LastName = request.LastName, Email = request.Email, Status = UserStatuses.Verified, Role = request.Role, PasswordHash = passwordHash, PasswordSalt = passwordSalt, RefreshToken = _tokenService.GenerateRefreshToken() }; await _userRepository.AddAsync(user); await _unitOfWork.SaveChangesAsync(); var token = _tokenService.GenerateToken(user.Email); var loggedUserDto = _mapper.Map <User, LoggedUserDto>(user); loggedUserDto.Token = token; return(new ResultResponse <LoggedUserDto>(loggedUserDto)); }
public Tuple <Users, string> CreateUser(string username, string email, string password, int[] roles) { var existingUser = _userRepository.GetSingleByUsernameOrEmail(username, email); if (existingUser != null) { return(new Tuple <Users, string>(null, "Username or Email already in use")); } var passwordSalt = _encryptionService.CreateSalt(); var user = new Users() { Username = username, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), DateCreated = DateTime.Now }; _userRepository.Insert(user); _userRepository.Commit(); if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } } _userRoleRepository.Commit(); return(new Tuple <Users, string>(user, string.Format("{0} - {1}", user.Username, "Registered Successfully"))); }
public tbl_tenant CreateTenant(string tenantkey, string tenantname, int tenanttype, string contactperson, string email, string contactno, string altcontactno, string password, int[] roles, DateTime accountvalidtill) { var existingTenant = _tenantRepository.GetSingleByTenantKey(tenantkey); if (existingTenant != null) { throw new Exception("Tenant is already registered!"); } var passwordSalt = _encryptionService.CreateSalt(); var newtenant = new tbl_tenant() { tenant_key = tenantkey, tenant_name = tenantname, tenant_type = tenanttype, contact_person = contactperson, email = email, contact_no = contactno, alt_contact_no = altcontactno, IsLocked = false, account_valid_till = accountvalidtill, date_created = DateTime.Now, created_by = 1, date_modified = DateTime.Now, modified_by = 1 }; var newuser = new tbl_user() { userid = tenantkey, user_name = tenantname, salt = passwordSalt, email = email, is_locked = false, is_tenant = true, password = _encryptionService.EncryptPassword(password, passwordSalt), date_created = DateTime.Now, date_modified = DateTime.Now }; _tenantRepository.Add(newtenant); _userRepository.Add(newuser); _unitOfWork.Commit(); if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(newuser, role); } } _unitOfWork.Commit(); return(newtenant); }
public void Should_Generate_Salt() { var salt = _encryptionService.CreateSalt(); Assert.NotNull(salt); Assert.Equal(SaltLength, salt.Length); }
public MessageDto Register(RegisterDto registerDto) { // var errors = CheckIfModelStateIsValid(registerDto); var errors = ModelStateValidator <RegisterDto> .CheckIfModelStateIsValid(registerDto); if (errors.Count != 0) { return(new MessageDto(false, errors[0].ErrorMessage)); } if (_userRepository.GetUserByName(registerDto.Username) != null) { return(new MessageDto(false, "Username is in use")); } var passwordSalt = _encryptionService.CreateSalt(); var user = CreateUser(registerDto.Username, registerDto.Password, passwordSalt, registerDto.Email, registerDto.Ime, registerDto.Prezime, registerDto.Razina); _userRepository.AddUser(user); if (registerDto.VjestineKorisnikaId != null) { SaveUserSpecijalnosti(user.Id, registerDto.VjestineKorisnikaId); } return(new MessageDto(true, "Successful registration")); }
public User CreateUser(string username, string email, string password, ICollection <int> roles) { User existingUser = _userService.GetUserByUsername(username); if (existingUser != null) { throw new UserAlreadyRegisteredException(); } string passwordSalt = _encryptionService.CreateSalt(); User user = new User() { Id = 1, Username = username, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now }; _userService.Create(user); foreach (int roleId in roles) { AddUserToRole(user, roleId); } return(user); }
public User CreateUser(string username, string password) { var existingUser = _userRepository.GetSingleByUsername(username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { Username = username, Salt = passwordSalt, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), CreatedOn = DateTime.Now }; _userRepository.Add(user); _unitOfWork.Commit(); return(user); }
public async Task <PwUser> CreateUserAsync(SignUpDto signUpDto) { await CheckUserRegistred(signUpDto); var passwordSalt = _encryptionService.CreateSalt(); int startBalance; try { startBalance = int.Parse(_configuration.GetSection("User:StartBalance").Value); } catch (Exception) { throw new PWException(CanNotGetStartBalanceMessage); } var user = new PwUser() { UserName = signUpDto.UserName, Salt = passwordSalt, Email = signUpDto.Email, PasswordHash = _encryptionService.EncryptPassword(signUpDto.Password, passwordSalt), Balance = startBalance }; await _userRepository.AddAsync(user); return(user); }
public ActionResult CreateUser(string username, string email, string firstname, string lastname, string wachtwoord, bool admin) { var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { Username = username, FirstName = firstname, LastName = lastname, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(wachtwoord, passwordSalt), DateCreated = DateTime.Now }; _data.Add(user); _data.Commit(); string roleName = admin? "Admin" : "Redacteur"; Role role = _data.GetSingle <Role>(r => r.Name.Equals(roleName)); var userRole = new UserRole() { RoleId = role.Id, UserId = user.Id }; _data.Add(userRole); _data.Commit(); return(Json(new { success = true, gebruiker = user })); }
public User CreateUser(string username, string email, string password, int[] roles) { var existingUser = _userRepository.GetUserByUsername(username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User { Username = username, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), DateCreated = DateTime.Now }; _userRepository.Add(user); _dbContext.SaveChanges(); if (roles != null && roles.Length > 0) { foreach (var role in roles) { AddUserToRole(user, role); } } _dbContext.SaveChanges(); return(user); }
public void CreateUser(UserViewModel ApplicationUserView) { ApplicationUser Exists = UserRepository.Get(x => x.UserName == ApplicationUserView.UserName && x.Email == ApplicationUserView.Email).FirstOrDefault(); if (Exists != null) { throw new Exception("ApplicationUsername & Email already in use"); } string SecurityStamp = EncryptionService.CreateSalt(); string HassedPassword = EncryptionService.EncryptPassword(ApplicationUserView.HashedPassword, SecurityStamp); ApplicationUserView.HashedPassword = HassedPassword; ApplicationUserView.Salt = SecurityStamp; ApplicationUser ApplicationUser = new ApplicationUser() { UserName = ApplicationUserView.UserName, Email = ApplicationUserView.Email, PasswordHash = ApplicationUserView.HashedPassword, SecurityStamp = ApplicationUserView.Salt, Active = ApplicationUserView.Active }; UserRepository.Add(ApplicationUser); SaveUser(); }
public int CreateUser(RegisterUserModel registerUserModel) { using (var scope = new TransactionScope()) { string salt = _encryptionService.CreateSalt(); string hashedPassword = _encryptionService.EncryptPassword(registerUserModel.Password, salt); var user = new User { Username = registerUserModel.Username, Salt = salt, HashedPassword = hashedPassword, CreationDate = BusinessSettings.ServerNow, GSM = registerUserModel.GSM, Country = registerUserModel.Country, City = registerUserModel.City, IsBlocked = false, IsEmailApproved = false, IsGpsEnabled = registerUserModel.IsGpsEnabled, IsGsmApproved = false, UserGuid = Guid.NewGuid() }; _unitOfWork.UserRepository.Add(user); _unitOfWork.Save(); scope.Complete(); return(user.Id); } }
public User CreateUser(string username, string email, string password, int[] roles) { var existingUser = _userRepository.GetSingleByUserName(username); if (existingUser != null) { throw new Exception("User is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { Username = username, Email = email, Salt = passwordSalt, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), DateCreated = DateTime.Now }; _userRepository.Add(user); _unitOfWork.Commit(); if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } } _unitOfWork.Commit(); return(user); }
public async Task <BaseResponse> RegisterAsync(RegisterRequest request) { var user = await _userRepository.GetAsync(user => user.Email == request.Email); if (user != null) { return(new ResultResponse <LoggedUserDto>("User with this email is already exist")); } var passwordSalt = _encryptionService.CreateSalt(); var passwordHash = _encryptionService.CreateHash(request.Password, passwordSalt); user = new User { FullName = request.FullName, Email = request.Email, PasswordHash = passwordHash, PasswordSalt = passwordSalt, }; await _userRepository.AddAsync(user); await _unitOfWork.SaveChangesAsync(); return(new BaseResponse()); }
public void Register(UserDTO userDto) { if (IsExistByEmail(userDto.Email) && IsExistByUsername(userDto.Username)) { throw new ValidationException("User with such email or username already exists", string.Empty); } var user = Mapper.Map <User>(userDto); user.Id = Guid.NewGuid(); if (!string.IsNullOrEmpty(userDto.PasswordHash)) { user.PasswordSalt = _encryptionService.CreateSalt(); user.PasswordHash = _encryptionService.EncryptPassword(userDto.PasswordHash, user.PasswordSalt); } user.CreateDate = DateTime.UtcNow; user.Roles = (userDto.Translates == null) ? new List <Role>() { _unitOfWorks.Repository <Role>().FindBy(x => x.Translates.Any()).First(x => x.Translates.Any(z => z.Name == "User")) } : _unitOfWorks.Repository <Role>() .FindBy(x => x.Translates.Any()).Where(x => x.Translates.Any(z => userDto.Translates.Any(t => t.RolesName.Contains(z.Name)))) .ToList(); _unitOfWorks.Repository <User>().Add(user); }
public User CreateUser(User user, string password, int[] roles) { var existUser = _userRepository.GetSingleByUsername(user.Username); if (existUser != null) { throw new ApplicationException("Username is already in use."); } var passwordSalt = _encryptionService.CreateSalt(); var userToAdd = new User() { Username = user.Username, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, CreatedDate = DateTime.Now, Salt = passwordSalt, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt) }; _userRepository.Add(userToAdd); _unitOfWork.Commit(); if (roles != null && roles.Length > 0) { foreach (var role in roles) { AddUserToRole(userToAdd, role); } _unitOfWork.Commit(); } return(userToAdd); }
public bool Register(LoginDto model) { var passwordSalt = _encryptionService.CreateSalt(); var user = CreateUser(model.id, "nebitno", passwordSalt, model.name); _userRepository.AddUser(user); return(true); }
public virtual void Insert(Profile instance) { if (instance == null) { throw new ArgumentNullException(this.ToString()); } instance.PasswordSalt = _encryptionService.CreateSalt(24); instance.Password = _encryptionService.Encrypt(instance.Password, instance.PasswordSalt); _repository.Insert(instance); }
public virtual void Insert(Client client) { if (client == null) { throw new ArgumentNullException("Client"); } if (client.PasswordSalt == null) { client.PasswordSalt = _encryptionService.CreateSalt(24); } _clientRepository.Insert(client); }
public User CreateUser(string username, string email, string password, string phone, string sex, string remarks, string address, int roleId, int companyInfoId, int clientNoticeId, int[] roles) { var existingUser = _userRepository.GetSingleByUsername(username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { UserName = username, RoleId = roleId, CompanyInfoId = companyInfoId, ClientNoticeId = clientNoticeId, Phone = phone, Sex = sex, Remarks = remarks, Address = address, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), DateCreated = DateTime.Now }; _userRepository.Add(user); _unitOfWork.Commit(); if (roleId != 0) { if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } } } _unitOfWork.Commit(); return(user); }
public User CreateUser(User user) { var existingUser = _userRepository.Get(t => t.Email == user.Email); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); user.Salt = passwordSalt; user.Password = _encryptionService.EncryptPassword(user.Password, passwordSalt); _userRepository.InsertAndSubmit(user); return(user); }
public User CreateUser(string username, string email, string password, string fullname, string phoneNumber, int?predicateId) { var existingUser = _userRepository.GetByUsername(username); if (existingUser != null) { throw new Exception("Username is already in use"); } existingUser = _userRepository.GetByEmail(email); if (existingUser != null) { throw new Exception("Email is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var hashedPassword = _encryptionService.EncryptPassword(password, passwordSalt); var user = new User() { Username = username, Salt = passwordSalt, Email = email, IsLocked = false, FullName = fullname, AccessFailedCount = 0, PhoneNumber = phoneNumber, EmailConfirmed = false, IsSystemEntity = false, IsDeleted = false, PhoneNumberConfirmed = false, TwoFactorEnabled = false, HashedPassword = hashedPassword }; _userRepository.Create(user); if (predicateId.HasValue && predicateId != 0) { AssignPredicateToUser(user, predicateId.Value); } return(user); }
public User CreateUser(string email, string password) { var passwordSalt = _encryptionService.CreateSalt(); var user = new User { Email = email, Password = _encryptionService.EncryptPassword(password, passwordSalt), Salt = passwordSalt, IsConfirmEmail = false, ConfirmCode = Guid.NewGuid().ToString(), Balance = 0, RoleId = (int)Roles.User }; _unitOfWork.UserRepository.Add(user); _unitOfWork.SaveChanges(); return(user); }
public Users Add(Users request) { int baseId = 100000; var maxId = _context.Users.AsEnumerable().OrderByDescending(x => x.id).FirstOrDefault(); int empId = baseId + Convert.ToInt16(maxId.id) + 1; request.empId = Convert.ToString(empId); request.isLearner = 1; request.salt = _encryptionService.CreateSalt(); request.password = _encryptionService.EncryptPassword(request.password, request.salt); request.hireDate = DateTime.Now; request.createdAt = DateTime.Now; request.updatedAt = DateTime.Now; var users = _context.Users.Add(request); _context.SaveChangesAsync(); return(request); }
public ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { var existingUser = _userRepository.GetAll().FirstOrDefault(x => x.Username == model.Username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { Username = model.Username, Salt = passwordSalt, Email = model.Email, IsLocked = 0, Password = _encryptionService.EncryptPassword(model.Password, passwordSalt), CreatedDate = DateTime.Now, UniqueId = Guid.NewGuid() }; _userRepository.Add(user); _unitOfWork.Commit(); int[] roles = new int[] { 3 }; if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } } _unitOfWork.Commit(); } return(View(model)); }
public User CreateUser(UserView userView) { var existingUser = _userRepository.GetSingleByUsername(userView.Email); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); Guid Id = Guid.NewGuid(); var user = new User() { Email = userView.Email, Salt = passwordSalt, Password = _encryptionService.EncryptPassword(userView.Password, passwordSalt), UserID = Id, ApprovedBy = Id, VerifiedEmail = false, VerifiedPhone = false, IsActive = false, IsDeleted = false, ModifiedOn = DateTime.Now, CreatedOn = DateTime.Now }; _userRepository.Add(user); _unitOfWork.Commit(); //if (roles != null || roles.Length > 0) //{ // foreach (var role in roles) // { // addUserToRole(user, role); // } //} _unitOfWork.Commit(); return(user); }
public UserEntity CreateUser(UserEntity userentity, int[] roles) { var existingUser = _userRepository.GetSingleByUsername(userentity.name); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new UserEntity() { name = userentity.name, salt = passwordSalt, email = userentity.email, islocked = false, password = _encryptionService.EncryptPassword(userentity.password, passwordSalt), createdate = DateTime.Now, birthday = DateTime.Now, mobile = userentity.mobile, truename = userentity.truename, sex = userentity.sex }; _userRepository.Add(user); _unitOfWork.Commit(); /* * if (roles != null || roles.Length > 0) * { * foreach (var role in roles) * { * addUserToRole(user, role); * } * }*/ // _unitOfWork.Commit(); return(user); }
public async Task <User> RegisterAsync(string username, string email, string password, int[] roles) { var userCheck = await _userRepository.GetSingleByUsernameAsync(username); if (userCheck != null) { throw new Exception("User with this username already exists."); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User { Username = username, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), DateCreated = DateTime.Now }; await _userRepository.AddAsync(user); await _userRepository.CommitAsync(); if (roles != null || roles.Length > 0) { foreach (var role in roles) { AddUserToRole(user, role); } } await _userRepository.CommitAsync(); return(user); }
public User CreateUser(string username, string email, string password, int[] roles) { var existingUser = _userRepository.GetAll().FirstOrDefault(x => x.Username == username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { Username = username, Salt = passwordSalt, Email = email, IsLocked = 0, Password = _encryptionService.EncryptPassword(password, passwordSalt), CreatedDate = DateTime.Now }; _userRepository.Add(user); _unitOfWork.Commit(); if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } } _unitOfWork.Commit(); return(user); }
public User CreateUser(RegistrationRequest regReq) { var existingUser = _userRepository.GetUserByMobile(regReq.MobileNumber); if (existingUser != null) { throw new Exception("Mobile No is already in use"); } var _passwordSalt = _encryptionService.CreateSalt(); var _hashPassword = _encryptionService.EncryptPassword(regReq.Password, _passwordSalt); var _userRole = UserRole.Member.ToString(); Guid Id = Guid.NewGuid(); var user = new User() { SponserID = CreateSponserID(), ParentSponserID = regReq.ParentSponserID, FirstName = regReq.FirstName, LastName = regReq.LastName, ContactNumber = regReq.MobileNumber, EmailID = regReq.EmailID, Gender = regReq.Gender, City = regReq.City, UserRole = _userRole, PasswordSalt = _passwordSalt, HashPassword = _hashPassword, IsActive = true, IsDeleted = false, ModifiedOn = DateTime.MinValue, CreatedOn = DateTime.Now }; _userRepository.Add(user); _unitOfWork.Commit(); return(user); }