Esempio n. 1
0
        private void ManageUserSessions(int pSessionID, PersonEN pPerson)
        {
            int    quantityMobile   = 0;
            int    quantityOther    = 0;
            string lastMobileDevice = "";
            string lastOtherDevice  = "";

            try
            {
                var sessions = sessionDAL.GetActiveUserSessions(pPerson.PersonID, true);
                if (sessions != null)
                {
                    if (sessions.Count >= 2)
                    {
                        //Cuenta cuantos dispositivos estan activos por tipo
                        foreach (var item in sessions)
                        {
                            if (item.MobileDevice)
                            {
                                quantityMobile++;
                            }
                            else
                            {
                                quantityOther++;
                            }
                        }

                        var lastDevice = sessions.ElementAt(1); //Obtiene el ultimo segundo de la lista

                        if (lastDevice.MobileDevice)
                        {
                            lastMobileDevice = lastDevice.DeviceID;
                        }
                        else
                        {
                            lastOtherDevice = lastDevice.DeviceID;
                        }

                        string vendorFullname = String.Format("{0} {1}", pPerson.Firstname, pPerson.Lastname);

                        if (quantityMobile > 1)
                        {
                            //Modifica el estado de la penultima sesión 'Activa' a 'Inactiva'
                            //en el último dispositivo Movil
                            var closeSession = sessionDAL.ClosePenultimateSession(pPerson.PersonID, true);

                            if (lastMobileDevice != pPerson.DeviceID)
                            {
                                if (!Boolean.Parse(ConfigurationManager.AppSettings["SilentSigninMode"]))
                                {
                                    emailSender.EmailNewLogin(vendorFullname, pPerson.Email, pPerson.DeviceInfo, CentralAmericaDateTime(DateTime.Now), ipAddressClient.GetLocationByIpAddress(pPerson.DeviceIp));
                                }
                            }
                        }

                        if (quantityOther > 1)
                        {
                            var closeSession = sessionDAL.ClosePenultimateSession(pPerson.PersonID, false);

                            if (lastOtherDevice != pPerson.DeviceID)
                            {
                                if (!Boolean.Parse(ConfigurationManager.AppSettings["SilentSigninMode"]))
                                {
                                    emailSender.EmailNewLogin(vendorFullname, pPerson.Email, pPerson.DeviceInfo, CentralAmericaDateTime(DateTime.Now), ipAddressClient.GetLocationByIpAddress(pPerson.DeviceIp));
                                }
                            }
                        }
                    }
                }
                else
                {
                    EventViewerLoggerBL.LogError("No sessions were found for current user");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                EventViewerLoggerBL.LogError("ManageUserSessions " + ex.Message);
            }
        }