Added more validation to the various AuthController functions.

This commit is contained in:
2021-01-15 09:41:35 -06:00
parent 8e9824f497
commit 5a9d3621fe
4 changed files with 68 additions and 15 deletions
+32 -1
View File
@@ -11,6 +11,7 @@ namespace SecureCore.Services
{
//
public static int UserNameMaxLength { get; } = 64;
public static int EmailMaxLength { get; } = 512;
public static (bool IsValid, string Message) IsUsernameValid(string userName)
{
@@ -29,7 +30,6 @@ namespace SecureCore.Services
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);
@@ -47,5 +47,36 @@ namespace SecureCore.Services
return Convert.ToInt32(result);
}
public static bool IsEmailInUse(string email, string connectionString)
{
using var connection = new SqlConnection(connectionString);
using var command = new SqlCommand("SELECT dbo.EmailInUse(@Email) AS InUse", connection);
command.Parameters.AddWithValue("Email", email);
connection.Open();
return Convert.ToBoolean(command.ExecuteScalar());
}
public static (bool IsValid, string Message) IsEmailValid(string email)
{
if (email.Length > EmailMaxLength) return (false, $"Email exceeds the maximum length of {EmailMaxLength} characters.");
//Code taken from user "Cogwheel" on StackOverflow: https://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address
try
{
var addr = new System.Net.Mail.MailAddress(email);
if (addr.Address != email) return (false, $"The provided email is not in the correct format.");
return (true, string.Empty);
}
catch
{
return (false, $"The provided email is not in the correct format.");
}
}
}
}