The following code is not working correctly. It is supposed to be associating an ip address of a user with a post's content, which in an online scenareo you know can be two different ip addresses. It is therefore showing exactly what was done with an account in the event of a password cracking scenareo. It is supposed to display what the hacker did while you were out. It is just a sample program, and if you could troubleshoot my linq that would be great. What it is doing now is not correctly taking into account the date and time of the next entry in the list.
/*Author: XXX
Purpose: To create a program that tracks the posts of disperate parties logging into an application in order to determine culpability.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DualPersonalitiesCulpability {
class Program {
static void Main(string[] args) {
List<Post> posts = new List<Post>();
User user = new User("Joe Schmoe");
DateTime dateOccurred = DateTime.Now - TimeSpan.FromDays(-4);
user.AddLoginEvent("192.168.1.1", DateTime.Now - TimeSpan.FromDays(-1));
Post post = new Post(user, "Something Good... ", dateOccurred.AddMinutes(15));
posts.Add(post);
dateOccurred = DateTime.Now - TimeSpan.FromDays(-3);
user.AddLoginEvent("192.168.1.2", dateOccurred);
post = new Post(user, "Something Bad... ", dateOccurred.AddMinutes(15));
posts.Add(post);
//dateOccurred = DateTime.Now - TimeSpan.FromDays(-2);
//user.AddLoginEvent("192.168.1.1", dateOccurred);
//post = new Post(user, "Something Good... ", dateOccurred.AddMinutes(15));
//posts.Add(post);
//dateOccurred = DateTime.Now - TimeSpan.FromDays(-1);
//user.AddLoginEvent("192.168.1.2", dateOccurred);
//post = new Post(user, "Something Bad... ", dateOccurred.AddMinutes(15));
//posts.Add(post);
//var ipAndDate = user.LoginEvents.Select(usr => new { IpAddress = usr.IpAddress, DateOccurred = usr.DateOccurred, Usr = usr });
posts = posts.OrderByDescending(p => p.DateOccurred).ToList();
var query = user.LoginEvents.Join(posts, x => x.User, y => y.User, (x, y) => new { Login = x, Post = y })
.Where(obj => obj.Post.DateOccurred > obj.Login.DateOccurred)
.Join(user.LoginEvents, x => x.Login.User, y => y.User, (x, y) => new { Post = x.Post, Login = x.Login, LoginY = y })
//.Where(obj => obj.Post.DateOccurred < obj.LoginY.DateOccurred)
.Select(obj => new { IpAddress = obj.Login.IpAddress, Content = obj.Post.Content, DateOccurred = obj.Post.DateOccurred, NextLogin = obj.LoginY });
//loop over query
foreach (var entry in query) {
Console.WriteLine(String.Format("{0}-{1}->{2}->{3}", entry.IpAddress, entry.DateOccurred, entry.Content, entry.NextLogin.ToString()));
}//end loop
Console.Write("Press any key to continue... ");
Console.ReadKey();
}//end method
}//end class
/// <summary>
/// A message from one user to another.
/// </summary>
public class Post {
/// <summary>
/// Unique database identifier for the post table.
/// </summary>
public int PostId {
get; set;
}
/// <summary>
/// The content of the message.
/// </summary>
public String Content {
get; set;
}
/// <summary>
/// User profile responsible for post.
/// </summary>
public User User {
get; set;
}
/// <summary>
/// The date and time this post occurred.
/// </summary>
public DateTime DateOccurred {
get; set;
}
public Post(User user, String content, DateTime dateOccurred) {
this.Content = content;
this.User = user;
this.DateOccurred = dateOccurred;
}//end method
/// <summary>
/// Display a string representation of the object.
/// </summary>
/// <returns></returns>
public override string ToString() {
return String.Format("POST: {0} {1}", this.User.ToString(), this.DateOccurred);
}//end method
}//end class
/// <summary>
/// Represents a user login.
/// </summary>
public class User {
/// <summary>
/// Unique database identifier for the User table.
/// </summary>
public int UserId {
get; set;
}
/// <summary>
/// Name of the user.
/// </summary>
public String Name {
get; set;
}
public User(String name) {
this.Name = name;
//sanity check
if (LoginEvents == null)
LoginEvents = new List<LoginEvent>();
}//end method
/// <summary>
/// Represents a history of the user logging onto the system.
/// </summary>
public IList<LoginEvent> LoginEvents {
get; set;
}
/// <summary>
/// Create a login event for this user.
/// </summary>
/// <param name="evt"></param>
private void AddLoginEvent(LoginEvent evt) {
this.LoginEvents.Add(evt);
}//end method
/// <summary>
/// Create a login event for this user.
/// </summary>
/// <param name="evt"></param>
public void AddLoginEvent(String ipAddress, DateTime dateOccurred) {
LoginEvent evt = new LoginEvent(this, ipAddress, dateOccurred);
AddLoginEvent(evt);
}//end method
/// <summary>
/// Displays a string represenation of the object.
/// </summary>
/// <returns></returns>
public override string ToString() {
return String.Format("{0}", this.Name);
}//end method
}//end class
/// <summary>
/// Represents the user logging into the system.
/// </summary>
public class LoginEvent {
/// <summary>
/// Unique database identifier for the LoginEvent table.
/// </summary>
public int LoginEventId {
get; set;
}
/// <summary>
/// The ip address of the user logging onto the system.
/// </summary>
public String IpAddress {
get; set;
}
/// <summary>
/// The date the user logged onto the system.
/// </summary>
public DateTime DateOccurred {
get; set;
}
/// <summary>
/// The user logging onto the system.
/// </summary>
public User User {
get; set;
}
public LoginEvent(User user, String ipAddress) {
this.User = user;
this.IpAddress = ipAddress;
this.DateOccurred = DateTime.Now;
}//end method
public LoginEvent(User user, String ipAddress, DateTime dateOccurred) {
this.User = user;
this.IpAddress = ipAddress;
this.DateOccurred = dateOccurred;
}//end method
/// <summary>
/// Displays a string representation of the object.
/// </summary>
/// <returns></returns>
public override string ToString() {
return String.Format("{0} -> {1}", User.ToString(), this.DateOccurred);
}//end method
}//end class
}//end namespace