/// <summary> /// Returns the user that has the same username and password given. /// throws ArgumentNull exception and Invalid Username Exception /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public static User GetUserWithUsernameAndPassword(string userName, string password) { if (string.IsNullOrEmpty(userName)) { throw new System.ArgumentNullException(nameof(userName)); } if (string.IsNullOrEmpty(password)) { throw new System.ArgumentNullException(nameof(password)); } using (SportsChatQueries query = new SportsChatQueries(new SportsChatEntities())) { Guid? guid = query.GetGuidByUserName(userName); if (!guid.HasValue) { throw new InvalidUserNameException("Couldn't get Guid", userName); } string hashedPassword = CreateHashedPassword(password, guid.Value); User u = query.GetUserByUserNameAndPasswordHash(userName, hashedPassword); if (u == null) { throw new InvalidUserNameAndPasswordException("User was not found", userName); } return u; } }
/// <summary> /// Creates a New User and adds that user to the db /// Returns the id of the User created /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="email"></param> /// <param name="firstName"></param> /// <param name="lastName"></param> public static int CreateNewUser(string userName, string password, string email, string firstName, string lastName) { User u = new User() { UserName = userName, EmailAddress = email, FirstName = firstName, LastName = lastName }; u.Guid = NewGuid(); u.Password = CreateHashedPassword(password, u.Guid); using (SportsChatQueries query = new SportsChatQueries(new SportsChatEntities())) { query.AddUser(u); User created = query.GetUserByUserNameAndPasswordHash(u.UserName, u.Password); if (created == null) { return 0; } return created.Id; } }