/// <summary> /// Writes user's data into database. /// </summary> /// <param name="user"></param> /// <returns></returns> public static bool RegisterUser(RegisterUserModel user) { SqlConnection connection = new SqlConnection(DataConnections.OwnerUAconnectionString); connection.Open(); // giving all user data needed for registration SqlCommand registerUserCommand = new SqlCommand("dbo.Users_RegisterUser", connection); registerUserCommand.CommandType = System.Data.CommandType.StoredProcedure; registerUserCommand.Parameters.AddWithValue("@FirstName", user.FirstName); registerUserCommand.Parameters.AddWithValue("@LastName", user.LastName); registerUserCommand.Parameters.AddWithValue("@Phone", user.Phone); registerUserCommand.Parameters.AddWithValue("@Email", user.Email); registerUserCommand.Parameters.AddWithValue("@Hash", BCryptHashing.HashPassword(user.Password)); // executing stored procedure, saving number of rows it affected int rowsAffected = registerUserCommand.ExecuteNonQuery(); connection.Close(); // operation was successful if data was written into database return(rowsAffected > 0); }
/// <summary> /// Gets user personal data if he entered email and password correctly. /// </summary> /// <param name="email"></param> /// <param name="password"></param> /// <returns></returns> public static FrontendUserModel GetUserByLoginInfo(string email, string password) { SqlConnection connection = new SqlConnection(DataConnections.OwnerUAconnectionString); connection.Open(); // user data to return FrontendUserModel user = null; SqlCommand getUserByLoginInfoCommand = new SqlCommand("dbo.Users_GetUserByEmail", connection); getUserByLoginInfoCommand.CommandType = System.Data.CommandType.StoredProcedure; getUserByLoginInfoCommand.Parameters.AddWithValue("@Email", email); SqlDataReader userDataReader = getUserByLoginInfoCommand.ExecuteReader(); // if there is user with such email and his password correct then creating new user entity while (userDataReader.Read() && BCryptHashing.IsPasswordValid(password, (string)userDataReader[5])) { user = new FrontendUserModel { FirstName = (string)userDataReader[1], LastName = (string)userDataReader[2], Phone = (string)userDataReader[3], Email = (string)userDataReader[4] }; } userDataReader.Close(); connection.Close(); return(user); }