CREATE TABLE AuditLog ( AuditLogID INT IDENTITY(1,1) PRIMARY KEY, EventType NVARCHAR(50) NOT NULL, EventDescription NVARCHAR(255) NOT NULL, UserID INT NULL, IPAddress NVARCHAR(50), EventDate DATETIME NOT NULL );
CREATE INDEX IDX_AuditLog_EventDate ON AuditLog (EventDate); CREATE INDEX IDX_AuditLog_UserID ON AuditLog (UserID);
using System; using System.Data.SqlClient; using System.Web; public class AuditLogger { private readonly ConClass _conClass = new ConClass(); // Your database connection class public void LogAuditEvent(string eventType, string eventDescription, int? userId = null) { try { string query = @"INSERT INTO AuditLog (EventType, EventDescription, UserID, IPAddress, EventDate) VALUES (@EventType, @EventDescription, @UserID, @IPAddress, @EventDate)"; SqlParameter[] parameters = { new SqlParameter("@EventType", eventType), new SqlParameter("@EventDescription", eventDescription), new SqlParameter("@UserID", userId ?? (object)DBNull.Value), new SqlParameter("@IPAddress", HttpContext.Current.Request.UserHostAddress), new SqlParameter("@EventDate", DateTime.Now) }; _conClass.ExecuteNonQuery(query, parameters); } catch (Exception ex) { // Handle exception } } }
public void LogUserLogin(int userId, string email) { AuditLogger logger = new AuditLogger(); logger.LogAuditEvent("UserLogin", "User " + email + " logged in successfully.", userId); }
public void LogAdminLogin(int userId, string adminName) { AuditLogger logger = new AuditLogger(); logger.LogAuditEvent("AdminLogin", "Admin user " + adminName + " logged in successfully.", userId); }
public void LogUserDeletion(int adminId, int userId) { AuditLogger logger = new AuditLogger(); logger.LogAuditEvent("UserDeletion", "User with ID " + userId + " was deleted by admin.", adminId); }
public void LogPasswordReset(int userId) { AuditLogger logger = new AuditLogger(); logger.LogAuditEvent("PasswordReset", "User with ID " + userId + " has reset the password.", userId); }
public void UserLogin(string email, string password) { // Validate user credentials int userId = ValidateUser(email, password); if (userId > 0) { // Log the user login event AuditLogger logger = new AuditLogger(); logger.LogAuditEvent("UserLogin", "User " + email + " logged in successfully.", userId); // Set session and redirect Session["UserID"] = userId; Response.Redirect("UserDashboard.aspx"); } else { // Handle login failure } }
public void ResetPassword(int userId, string newPassword) { // Reset user password bool success = UpdateUserPassword(userId, newPassword); if (success) { // Log the password reset event AuditLogger logger = new AuditLogger(); logger.LogAuditEvent("PasswordReset", "User with ID " + userId + " has reset the password.", userId); // Notify user of success } else { // Handle reset failure } }