Added and tested the password reset feature. So far so good, needs some more validation but its working and that's a good first step.

This commit is contained in:
2021-01-13 16:41:20 -06:00
parent e198838a32
commit 6f8b7af78e
3 changed files with 51 additions and 23 deletions
+24 -10
View File
@@ -29,20 +29,22 @@ namespace SecureCore.Authentication
return (GetHash(password, salt), Convert.ToBase64String(salt));
}
public static (bool IsValid, string Message) IsPasswordValid(string password, string salt, string passwordHash)
public static (bool IsValid, string Message) IsPasswordValid(string password)
{
if (password.Length < MinPasswordLength) return (false, $"Your password is too short, it must be at least {MinPasswordLength} characters long and not exceed {MaxPasswordLength} characters.");
if (password.Length > MaxPasswordLength) return (false, $"Your password is too long, it must not exceed {MaxPasswordLength} characters and must contain at least {MinPasswordLength} characters.");
var saltBytes = Convert.FromBase64String(salt);
if (passwordHash == GetHash(password, saltBytes))
return (true, string.Empty);
else
return (false, "User name or password is not correct.");
return (true, string.Empty);
}
public static void InsertPasswordResetRequest(string userName, string sessionToken, DateTime expirationDate, string userAgent, string ipAddress, string connectionString)
public static bool IsPasswordAMatch(string password, string salt, string passwordHash)
{
var saltBytes = Convert.FromBase64String(salt);
return passwordHash == GetHash(password, saltBytes);
}
public static void InsertPasswordResetRequest(string email, string sessionToken, DateTime expirationDate, string userAgent, string ipAddress, string connectionString)
{
using var connection = new SqlConnection(connectionString);
@@ -50,7 +52,7 @@ namespace SecureCore.Authentication
command.Parameters.AddWithValue("SessionToken", sessionToken);
command.Parameters.AddWithValue("ExpirationDate", expirationDate);
command.Parameters.AddWithValue("UserName", userName);
command.Parameters.AddWithValue("Email", email);
command.Parameters.AddWithValue("UserAgent", userAgent);
command.Parameters.AddWithValue("IpAddress", ipAddress);
@@ -59,7 +61,19 @@ namespace SecureCore.Authentication
command.ExecuteNonQuery();
}
//TODO: Reset Password Function Here
public static void ResetPassword(string passwordHash, string salt, string sessionToken, string connectionString)
{
using var connection = new SqlConnection(connectionString);
using var command = new SqlCommand("ResetPassword", connection) { CommandType = CommandType.StoredProcedure };
command.Parameters.AddWithValue("PasswordHash", passwordHash);
command.Parameters.AddWithValue("Salt", salt);
command.Parameters.AddWithValue("SessionToken", sessionToken);
connection.Open();
command.ExecuteNonQuery();
}
public static (string PasswordHash, string Salt) GetPasswordHashAndSalt(string username, string connectionString)
{