using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace SecureCore.Services { public static class UserDataService { // public static int UserNameMaxLength { get; } = 64; public static (bool IsValid, string Message) IsUsernameValid(string userName) { if (userName.Length > UserNameMaxLength) return (false, $"Username too long, must not exceed {UserNameMaxLength} characters."); var invalidChars = new List(); foreach(var c in userName) if (char.IsPunctuation(c) || char.IsSymbol(c) || char.IsControl(c) || char.IsSeparator(c) || char.IsWhiteSpace(c)) invalidChars.Add(c); if (invalidChars.Count > 0) return (false, $"The character(s) '{string.Join(",", invalidChars)}' are not allowed in a user name."); return (true, string.Empty); } public static int RegisterNewUser(string userName, string email, string passwordHash, string saltHash, string sessionToken, DateTime expirationDate, string userAgent, string ipAddress, string connectionString) { using var connection = new SqlConnection(connectionString); using var command = new SqlCommand("RegisterNewUser", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("UserName", userName); command.Parameters.AddWithValue("Email", email); command.Parameters.AddWithValue("Password", passwordHash); command.Parameters.AddWithValue("Salt", saltHash); command.Parameters.AddWithValue("SessionToken", sessionToken); command.Parameters.AddWithValue("ExpirationDate", expirationDate); command.Parameters.AddWithValue("UserAgent", userAgent); command.Parameters.AddWithValue("IpAddress", ipAddress); connection.Open(); var result = command.ExecuteScalar(); return Convert.ToInt32(result); } } }