//Logs the session if it's a LogOn or LogOff event. public bool Log(SessionChangeDescription changeDescription, SessionProperties properties) { if (m_server == null) throw new InvalidOperationException("No MongoDB Connection present."); string username = "******"; if (properties != null) { UserInformation ui = properties.GetTrackedSingle<UserInformation>(); if ((bool)Settings.Store.UseModifiedName) username = ui.Username; else username = ui.OriginalUsername; } //Logon Event if (changeDescription.Reason == SessionChangeReason.SessionLogon) { if (m_server.State != MongoServerState.Connected) m_server.Connect(); string table = Settings.Store.SessionTable; string databaseName = Settings.Store.Database; var database = m_server.GetDatabase(databaseName); var collection = database.GetCollection<Event>(table); //Update the existing entry for this machine/ip if it exists. var query = Query.And( Query.NotExists("logoutstamp"), Query.EQ("machine", Environment.MachineName), Query.EQ("ipaddress", getIPAddress()) ); var update = Update.Set("logoutstamp", DateTime.UtcNow); collection.Update(query, update); //Insert new entry for this logon event var pGinaSession = new Session { username = username, machine = Environment.MachineName, ipaddress = getIPAddress(), loginstamp = DateTime.UtcNow }; collection.Insert(pGinaSession); m_logger.DebugFormat("Logged LogOn event for {0} at {1}", username, getIPAddress()); } //LogOff Event else if (changeDescription.Reason == SessionChangeReason.SessionLogoff) { if (m_server.State != MongoServerState.Connected) m_server.Connect(); string table = Settings.Store.SessionTable; string databaseName = Settings.Store.Database; var database = m_server.GetDatabase(databaseName); var collection = database.GetCollection<Event>(table); //Update the existing entry for this machine/ip if it exists. var query = Query.And( Query.NotExists("logoutstamp"), Query.EQ("username", username), Query.EQ("machine", Environment.MachineName), Query.EQ("ipaddress", getIPAddress()) ); var update = Update.Set("logoutstamp", DateTime.UtcNow); m_logger.DebugFormat("Logged LogOff event for {0} at {1}", username, getIPAddress()); } return true; }
//Tests the table based on the registry data. Returns a string indicating the table status. public string TestTable() { if (m_server == null) throw new InvalidOperationException("No MongoDB Connection present."); //Open the connection if it's not presently open if (m_server.State != MongoServerState.Connected) m_server.Connect(); string table = Settings.Store.SessionTable; string databaseName = Settings.Store.Database; var database = m_server.GetDatabase(databaseName); var collection = database.GetCollection<Event>(table); //Update the existing entry for this machine/ip if it exists. var pGinaSession = new Session { username = "******"}; collection.Insert(pGinaSession); return "Table exists and is setup correctly."; }