public JsonResult generateNewSession(string session, string email, int system) { using (var db = new Entities()) { Response response = null; if (!string.IsNullOrEmpty(email) && ValidaEmail(email)) { if (!string.IsNullOrEmpty(session)) { var participants = db.Sys_User.Where(z => z.dsEmail == email); List<Sys_User> listParticipant = participants.ToList(); if (listParticipant != null && listParticipant.Count > 0) { int active_session = 0; Sys_User participant = db.Sys_User.Find(listParticipant[0].idUser); if (participant.dtLastSession != null) { DateTime dateNow = DateTime.Now; TimeSpan timeSpan = dateNow.Subtract((DateTime)participant.dtLastSession); active_session = timeSpan.Minutes; } Sys_UserSession sysSession = participant.Sys_UserSession.Where(s => s.idUser == participant.idUser && s.idSystem == system).FirstOrDefault(); if (active_session <= 60 && sysSession.dsSession.Equals(session)) { string newSession = SessionController.New(email); SessionController.Write(newSession, participant.idUser, system); response = new Login(participant.idUser, participant.idRole.Value, participant.idPerson.Value, participant.idPerson.Value, participant.idMerchant.Value, system, participant.nmUser, email, newSession); } else { response = new ResponseFailure("invalid-session"); } } else { response = new ResponseFailure("invalid-email"); } } else { response = new ResponseFailure("invalid-session"); } } else { response = new ResponseFailure("invalid-email"); } return Json(response, JsonRequestBehavior.AllowGet); } }
public Response getLogin(string accessToken, string email,string jsonParams) { Response response = null; using (var db = new Entities()) { if (!string.IsNullOrEmpty(email) && ValidaEmail(email)) { if (!string.IsNullOrEmpty(accessToken) && TokenController.IsValidToken(accessToken, email)) { dynamic myObj; try { myObj = JsonConvert.DeserializeObject(jsonParams); string password = null; int idSystem = -1; string ipAddress = null; string dsAgent = null; if (myObj.password != null) password = myObj.password; //required if (myObj.system != null) idSystem = myObj.system; //required if (myObj.ipAddress != null) ipAddress = myObj.ipAddress; //required if (myObj.userAgent != null) dsAgent = myObj.userAgent; //required if (!string.IsNullOrEmpty(password)) { string newSession = SessionController.New(email); ObjectResult<Sys_UserLogin_Result> listUser = db.Sys_UserLogin(email, PasswordEncrypt(password), idSystem, ipAddress, dsAgent, accessToken, newSession); List<Sys_UserLogin_Result> listUserLogin = listUser.ToList(); if (listUserLogin != null && listUserLogin.Count > 0) { int idUser = -1; int idRole = -1; int idPerson = -1; int idEnterprise = -1; int idMerchant = -1; int idUserCreate = -1; int idUserUpdate = -1; DateTime expire_at = DateTime.MinValue; DateTime create_at = DateTime.MinValue; DateTime update_at = DateTime.MinValue; string session = string.Empty; string nmUser = string.Empty; string stUser = string.Empty; string dsEmail = string.Empty; idUser = (int)listUserLogin[0].idUser; if (listUserLogin[0].idRole != null) idRole = (int)listUserLogin[0].idRole; if (listUserLogin[0].idPerson != null) idPerson = (int)listUserLogin[0].idPerson; if (listUserLogin[0].idEnterprise != null) idEnterprise = (int)listUserLogin[0].idEnterprise; if (listUserLogin[0].idMerchant != null) idMerchant = (int)listUserLogin[0].idMerchant; if (listUserLogin[0].idUserCreate != null) idUserCreate = (int)listUserLogin[0].idUserCreate; if (listUserLogin[0].idUserLastUpdate != null) idUserUpdate = (int)listUserLogin[0].idUserLastUpdate; nmUser = (string)listUserLogin[0].nmUser; dsEmail = (string)listUserLogin[0].dsEmail; stUser = (string)listUserLogin[0].stUser; session = newSession; SessionController.Write(newSession, idUser, idSystem); if (listUserLogin[0].dtExpire != null) expire_at = (DateTime)listUserLogin[0].dtExpire; if (listUserLogin[0].dtCreate != null) create_at = (DateTime)listUserLogin[0].dtCreate; if (listUserLogin[0].dtLastUpdate != null) update_at = (DateTime)listUserLogin[0].dtLastUpdate; if (idUser > 0 && expire_at > DateTime.Now) { response = new Login(idUser, idRole, idPerson, idEnterprise, idMerchant, idSystem, nmUser, dsEmail, session); } else if (idUser == -1)/*Invalid Email*/ { response = new ResponseFailure("invalid-login"); } else if (idUser == -2)/*Invalid password*/ { response = new ResponseFailure("invalid-login"); } else if (idUser == -3)/*Invalid previleges*/ { response = new ResponseFailure("invalid-login"); } else if (idUser == -4)/*Invalid Expire Date*/ { response = new ResponseFailure("invalid-login"); } else { response = new ResponseFailure("invalid-login"); } } else { response = new ResponseFailure("invalid-login"); } } else { response = new ResponseFailure("invalid-login"); } } catch (Exception ex) { response = new ResponseFailure(ex.Message); } } else { response = new ResponseFailure("invalid-token"); } } else { response = new ResponseFailure("invalid-email"); } } return response; }
public JsonResult generateNewSession(string session, string email) { using (var db = new Entities()) { Response response = null; if (!string.IsNullOrEmpty(email) && ValidaEmail(email)) { if (!string.IsNullOrEmpty(session)) { var participants = db.Sys_User.Where(z => z.dsEmail == email); List<Sys_User> listParticipant = participants.ToList(); if (listParticipant != null && listParticipant.Count > 0) { int active_session = 0; Sys_User participant = db.Sys_User.Find(listParticipant[0].idUser); if (participant.dtLastSession != null) { DateTime dateNow = DateTime.Now; TimeSpan timeSpan = dateNow.Subtract((DateTime)participant.dtLastSession); active_session = timeSpan.Minutes; } if (active_session <= 60 && participant.dsSession.Equals(session)) { participant.dtLastSession = DateTime.Now; participant.dsSession = NewSession(email); db.Entry(participant).State = EntityState.Modified; db.SaveChanges(); response = new Login(participant.idUser, participant.idRole.Value, participant.idPerson.Value, participant.idPerson.Value, participant.idMerchant.Value, 0, participant.nmUser, email, participant.dsSession); } else { response = new ResponseFailure("invalid-session"); } } else { response = new ResponseFailure("invalid-email"); } } else { response = new ResponseFailure("invalid-session"); } } else { response = new ResponseFailure("invalid-email"); } return Json(response, JsonRequestBehavior.AllowGet); } }