public User CreateUser(string username, string email, string password, int[] roleIds) { var existingUser = m_userRepo.GetUserByUsername(username); if (existingUser != null) { throw new Exception("Username is already in user."); } var salt = m_encryptionService.CreateSalt(); var user = new User { Username = username, Salt = salt, Email = email, IsLocked = false, Password = m_encryptionService.Encrypt(password, salt), DateCreated = DateTime.Now }; m_userRepo.Add(user); m_unitOfWork.Commit(); if (roleIds != null && roleIds.Length > 0) { foreach (var roleId in roleIds) { AddUserToRole(user, roleId); } } m_unitOfWork.Commit(); return user; }
private bool CheckPasswrod(User user, string password) { return string.Equals(m_encryptionService.Encrypt(password, user.Salt), user.Password); }
private bool IsUserValid(User user, string password) { return CheckPasswrod(user, password) && !user.IsLocked; }
private void AddUserToRole(User user, int roleId) { var role = m_roleRepo.SingleById(roleId); if (role == null) { throw new ApplicationException("Role doesn't exist."); } var userRole = new UserRole { UserId = user.Id, RoleId = roleId }; m_userRoleRepo.Add(userRole); }