//on session end, 'log out' user protected void Session_End(object sender, EventArgs e) { if (Session["username"] != null) { String inactive_user = Session["username"].ToString(); OnlineChatDBEntities db = new OnlineChatDBEntities(); User current_user = db.Users.Where(u => u.username == inactive_user).FirstOrDefault(); current_user.is_session_active = false; db.SaveChanges(); } }
public ActionResult createMessage(MessageModel message) { OnlineChatDBEntities db = new OnlineChatDBEntities(); Message new_message = new Message(); new_message.message_body = message.body; new_message.date_posted = DateTime.UtcNow; String username = HttpContext.Session["username"].ToString(); new_message.fk_poster_id = db.Users.Where(u => u.username == username).FirstOrDefault().user_id; db.Messages.Add(new_message); db.SaveChanges(); message.id = new_message.msg_id; message.posted_by.username = username; message.date_posted = new_message.date_posted; //message.time_elapsed_since = MessageModel.getDateSince(new_message.date_posted); return Json(message, JsonRequestBehavior.AllowGet); }
public ActionResult setUsername(string username) { //nothing is done if provided username is blank if (username != "") { OnlineChatDBEntities db = new OnlineChatDBEntities(); //if an existing username is taken by session, set it as inactive if (Session["username"] != null && username != Session["username"].ToString()) setUsernameInactive(Session["username"].ToString()); //if username doesn't currently exist, create it in the database User target_user = db.Users.Where(u => u.username == username).FirstOrDefault(); if (target_user == null) { target_user = new User(); target_user.username = username; target_user.is_session_active = false; db.Users.Add(target_user); } //check if username is taken, if free, set session variable to username and session trait in db to true if (!isUsernameTaken(username, db)) { HttpContext.Session["username"] = username; target_user.is_session_active = true; db.SaveChanges(); return Json(username, JsonRequestBehavior.AllowGet); } } return Json(false, JsonRequestBehavior.AllowGet); }