public void Endafk(string sAFK_tmp) { string eAFK = DateTime.Now.ToLongTimeString(); var date_tmp = DateTime.Now.ToShortDateString(); string connectionId = Context.ConnectionId; if (context.SetSessionHubModel.Any(x => x.ConnectionId == connectionId)) { //Изменяем записи в бд при отключение SessionHubModel UPSetTimeUp = context.SetSessionHubModel.Where(u => u.ConnectionId == connectionId && u.Date == date_tmp).First(); if (UPSetTimeUp.AfkTime == null) { UPSetTimeUp.AfkTime = (TimeSpan.Parse(eAFK) - TimeSpan.Parse(sAFK_tmp)).ToString(); } else { UPSetTimeUp.AfkTime = (TimeSpan.Parse(UPSetTimeUp.AfkTime) + (TimeSpan.Parse(eAFK) - TimeSpan.Parse(sAFK_tmp))).ToString(); } UPSetTimeUp.IsAction = true; context.Entry(UPSetTimeUp).State = EntityState.Modified; context.SaveChanges(); } string DateNow = DateTime.Now.ToShortDateString(); List <SessionHubModel> sessionHubModels_tmp = context.SetSessionHubModel.Where(u => u.Date == DateNow).ToList(); List <SessionHubModel> result = sessionHubModels_tmp.Where(u => u.IsAction == true || (u.IsAction == false && u.EndTime == null)).ToList(); Clients.All.feAFK(result); }
// Отключение пользователя public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled) { var date_tmp = DateTime.Now.ToShortDateString(); string connectionId = Context.ConnectionId; if (context.SetSessionHubModel.Any(x => x.ConnectionId == connectionId)) { //Изменяем записи в бд при отключение SessionHubModel UPSetTimeUp = context.SetSessionHubModel.Where(u => u.ConnectionId == connectionId && u.Date == date_tmp).First(); UPSetTimeUp.EndTime = DateTime.Now.ToLongTimeString(); UPSetTimeUp.TimeInSystem = (TimeSpan.Parse(DateTime.Now.ToLongTimeString()) - TimeSpan.Parse(UPSetTimeUp.StartTime)).ToString(); UPSetTimeUp.IsAction = false; context.Entry(UPSetTimeUp).State = EntityState.Modified; context.SaveChanges(); var item = context.SetSessionHubModel.FirstOrDefault(x => x.ConnectionId == connectionId); Clients.All.onUserDisconnected(connectionId, item.UserName); } var itemChat = UsersChat.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId); if (itemChat != null) { UsersChat.Remove(itemChat); var id = Context.ConnectionId; Clients.All.onUserDisconnected(id, itemChat.Name); } return(base.OnDisconnected(stopCalled)); }
public ActionResult Login(LoginModel model) { string someDateTime = DateTime.Now.ToShortDateString(); if (ModelState.IsValid) { // поиск пользователя в бд User user = null; using (ApplicationContext db = new ApplicationContext()) { string password = CodePass(model.Password); user = db.SetUser.FirstOrDefault(u => u.Login == model.Login && u.Password == password); } if (user != null) { SessionHubModel date = null; using (ApplicationContext db = new ApplicationContext()) { date = db.SetSessionHubModel.FirstOrDefault(u => u.UserId == user.Id && u.Date == someDateTime && u.IsAction == true); } if (date == null) { FormsAuthentication.SetAuthCookie(model.Login, true); // Создать объект cookie-набора HttpCookie cookie = new HttpCookie("cookieAuth"); // Установить значения в нем cookie["Login"] = CryporEngine.Encrypt(model.Login, true); cookie.Expires = DateTime.Now.AddYears(1); // Добавить куки в ответ Response.Cookies.Add(cookie); return(RedirectToAction("_Index", "Home")); } else { ModelState.AddModelError("", "Пользователь Авторизирован"); } } else { ModelState.AddModelError("", "Пользователя с таким логином и паролем нет"); } } return(View(model)); }