public static bool ShouldUserLog(string username) { LoggerCWContext context = new LoggerCWContext(); foreach (Log line in context.Logs.ToList()) { if (line.ToString().StartsWith("Error login attempt")) { Regex usernameRegex = new Regex("Username: '******', Date: '(.*)', Attempt: '(.*)'"); Match match = usernameRegex.Match(line.ToString()); if (match.Success) { string usernameMatch = match.Groups[1].Value; if (username == usernameMatch) { int attemptMatch = int.Parse(match.Groups[3].Value); if (attemptMatch == 3) { string dateMatch = match.Groups[2].Value; DateTime lastTime = DateTime.ParseExact(dateMatch, DATETIME_FORMAT, null); DateTime currentTime = DateTime.Now; TimeSpan diff = currentTime - lastTime; double minutesDiff = diff.TotalMinutes; if (minutesDiff > 3) { LogActivity(string.Format("Error login attempt; Username: '******', Date: '{1}', Attempt: '0'", username, DateTime.Now.ToString(DATETIME_FORMAT), ++attemptMatch)); return(true); // return true, tumbstone log message } //reject enterance string msg = string.Format("User '{0}' cannot log in since three minutes have not passed since last failed login attempt: '{1}'", username, lastTime.ToString(DATETIME_FORMAT)); Console.WriteLine(msg); LogActivity(msg); return(false); } else { string msg = string.Format("Error login attempt; Username: '******', Date: '{1}', Attempt: '{2}'", username, DateTime.Now.ToString(DATETIME_FORMAT), ++attemptMatch); Console.WriteLine(msg); LogActivity(msg); //add log to increment attempt counter, return true } return(true); } } else { Console.WriteLine("Unsuccessful match failure."); } } } return(true); }
public static void LogActivity(string activity) { string activityLine = activity; if (activity.StartsWith("Error login attempt")) { activityLine += Environment.NewLine; } LoggerCWContext context = new LoggerCWContext(); context.Logs.Add(new Log(context.GetNextLogId(), activityLine)); context.SaveChanges(); currentSessionActivities.Add(activityLine); }
private static string GetLastLoginAttemptLog(string username) { LoggerCWContext context = new LoggerCWContext(); foreach (Log line in context.Logs.ToList()) { Regex usernameRegex = new Regex("Username: '******', Date: '(.*)', Attempt: '(.*)'"); Match match = usernameRegex.Match(line.ToString()); if (match.Success) { string usernameMatch = match.Groups[1].Value; if (usernameMatch == username) { return(line.ToString()); } } } return(null); }
private static int GetLoginAttempts(string username) { LoggerCWContext context = new LoggerCWContext(); foreach (Log line in context.Logs.ToList()) { Regex usernameRegex = new Regex("Username: '******', Date: '(.*)', Attempt: '(.*)'"); Match match = usernameRegex.Match(line.ToString()); if (match.Success) { string usernameMatch = match.Groups[1].Value; if (usernameMatch == username) { int attemptMatch = int.Parse(match.Groups[3].Value); return(attemptMatch); } } } return(0); }
public static string ViewLogs() { LoggerCWContext context = new LoggerCWContext(); return(string.Join(Environment.NewLine, context.Logs.ToList())); }