Cleaned up and refactored the code to make things much easier to understand.
This commit is contained in:
@@ -1,25 +1,14 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureCore.Authentication
|
||||
{
|
||||
public class SessionManager
|
||||
public static class SessionManager
|
||||
{
|
||||
private int SessionKeySize { get; } = 64; //n bytes
|
||||
private string ConnectionString { get; set; }
|
||||
private static int SessionKeySize { get; } = 64; //n bytes
|
||||
|
||||
public SessionManager()
|
||||
{
|
||||
if (AppSettingsManager.TryGetConnectionStringByName("MainDataConnectionString", out string connection))
|
||||
ConnectionString = connection;
|
||||
else
|
||||
throw new Exception("Failed to get connection string.");
|
||||
}
|
||||
|
||||
public string CreateSessionToken()
|
||||
public static string CreateSessionToken()
|
||||
{
|
||||
var token = new byte[SessionKeySize];
|
||||
|
||||
@@ -28,13 +17,50 @@ namespace SecureCore.Authentication
|
||||
return Convert.ToBase64String(token);
|
||||
}
|
||||
|
||||
//public string CreatePasswordRecoveryKey(string userName)
|
||||
//{
|
||||
public static bool IsSessionTokenValid(string sessionToken, string connectionString, bool isResetToken = false)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
|
||||
// using (var connection = new SqlConnection(ConnectionString))
|
||||
// {
|
||||
using var command = new SqlCommand("ValidateSessionToken", connection) { CommandType = CommandType.StoredProcedure };
|
||||
|
||||
// }
|
||||
//}
|
||||
command.Parameters.AddWithValue("SessionToken", sessionToken);
|
||||
command.Parameters.AddWithValue("IsResetToken", isResetToken);
|
||||
|
||||
connection.Open();
|
||||
|
||||
var result = command.ExecuteScalar();
|
||||
|
||||
return Convert.ToBoolean(result);
|
||||
}
|
||||
|
||||
public static void Logout(string sessionToken, string connectionString)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
|
||||
using var command = new SqlCommand("LogoutUser", connection) { CommandType = CommandType.StoredProcedure };
|
||||
|
||||
command.Parameters.AddWithValue("SessionToken", sessionToken);
|
||||
|
||||
connection.Open();
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public static void Login(string userName, string sessionToken, DateTime expirationDate, string userAgent, string ipAddress, string connectionString)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
|
||||
using var command = new SqlCommand("LoginUser", connection) { CommandType = CommandType.StoredProcedure };
|
||||
|
||||
command.Parameters.AddWithValue("SessionToken", sessionToken);
|
||||
command.Parameters.AddWithValue("ExpirationDate", expirationDate);
|
||||
command.Parameters.AddWithValue("UserName", userName);
|
||||
command.Parameters.AddWithValue("UserAgent", userAgent);
|
||||
command.Parameters.AddWithValue("IpAddress", ipAddress);
|
||||
|
||||
connection.Open();
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user