public ActionResult Login(LoginViewModel login)
        {
            Logger.Debug("Inside User Controller- Login HttpPost");
            string UpdatedSessionID = string.Empty;
            try
            {
                if (ModelState.IsValid)
                {
                    //MasterLogin aspuser = _IUserRepository.UserLoginServer(login.UserName, login.Password);
                    var aspuser = _IUserRepository.UserLogin(login.UserName, _IUserRepository.EncodeTo64(login.Password));
                    if (aspuser != null)
                    {

                        List<MasterLogin> masterlogins = new List<MasterLogin>();
                        MasterLogin lMasterLogin = new MasterLogin();
                        lMasterLogin.UserGUID = aspuser.UserGUID;
                        // Logger.Debug("Updatating MasterLogin Record" + aspuser.UserGUID);
                        lMasterLogin.LoginType = (short)eLoginType.WebLogin;
                        masterlogins = _IUserRepository.GetMasterLogin(lMasterLogin);

                        //var GeoloactionofCurrentIP = GeoIp.GetMy();
                        //double latitude = 0, longitude = 0, timezone = 0;
                        //if (GeoloactionofCurrentIP.KeyValue != null)
                        //{
                        //    latitude = Convert.ToDouble(GeoloactionofCurrentIP.KeyValue["Latitude"]);
                        //    longitude = Convert.ToDouble(GeoloactionofCurrentIP.KeyValue["Longitude"]);
                        //    timezone = getTimeZone(latitude, longitude);
                        //}

                        if (masterlogins != null && masterlogins.Count > 0)
                        {
                            #region masterlogins record available
                            lMasterLogin = masterlogins[0]; // Alok need to be fixed
                            // Update the Master Login
                            lMasterLogin.ExpiryTime = DateTime.UtcNow.AddYears(10);
                            Logger.Debug("Updatating MasterLogin Record");
                            UpdatedSessionID = _IUserRepository.UpdateMasterLogin(lMasterLogin);
                            if (!string.IsNullOrEmpty(UpdatedSessionID))
                            {
                                #region UpdatedSessionID is not null
                                Logger.Debug("Updated Session ID: " + UpdatedSessionID);
                                #endregion
                            }
                            else
                            {
                                Logger.Error("Unable to get the Session");
                            }
                            #endregion
                        }
                        else
                        {
                            #region masterlogins record not available
                            //Logger.Debug("Creating MasterLogin Record");
                            lMasterLogin.ExpiryTime = DateTime.UtcNow.AddYears(10);
                            if (CreateMasterLogin(lMasterLogin, ref UpdatedSessionID) > 0)
                            {
                                // UpdatedSessionID = lMasterLogin.SessionID;
                                Logger.Debug("New Session ID: " + UpdatedSessionID);
                            }
                            #endregion
                        }

                        HttpCookie currentLoggedInUser = Request.Cookies["LID"];
                        string PreviousLoginUserID = string.Empty;
                        if (currentLoggedInUser != null)
                        {
                            //Logger.Debug("currentLoggedInUser is not null");
                            PreviousLoginUserID = _IUserRepository.DecodeFrom64(currentLoggedInUser.Value);
                        }
                        else
                        {
                            //Logger.Debug("currentLoggedInUser is  null");
                            currentLoggedInUser = new HttpCookie("LID");
                        }
                        currentLoggedInUser.Value = _IUserRepository.EncodeTo64(aspuser.UserGUID.ToString());
                        // Logger.Debug("currentLoggedInUser value" + currentLoggedInUser.Value);
                        Response.Cookies.Add(currentLoggedInUser);

                        Session["UserGUID"] = aspuser.UserGUID.ToString();
                        Session["OrganizationGUID"] = _IOrganizationRepository.GetOrganizationIDByUserGUID(aspuser.UserGUID);
                        if (Session["OrganizationGUID"] != null && !string.IsNullOrEmpty(Session["OrganizationGUID"].ToString()))
                            Session["OrganizationName"] = _IOrganizationRepository.GetOrganizationByID(new Guid(Session["OrganizationGUID"].ToString())).OrganizationFullName;
                        Session["UserName"] = _IGlobalUserRepository.GetGlobalUserByID(aspuser.UserGUID).UserName;
                        Session["SessionID"] = UpdatedSessionID;
                        Session["UserType"] = _IGlobalUserRepository.GetUserType(aspuser.UserGUID);
                        // Logger.Debug("End of Session Assignment");
                        if (!string.IsNullOrEmpty(login.RedirectURL))
                        {
                            //return RedirectToAction(login.RedirectURL);
                            if (PreviousLoginUserID == aspuser.UserGUID.ToString())
                            {
                                login.RedirectURL = _IUserRepository.DecodeFrom64(login.RedirectURL);
                                Response.Redirect(login.RedirectURL);
                                return null;
                            }
                            else
                            {
                                return RedirectToAction("../User/Dashboard");
                            }
                        }
                        else
                        {
                            if (Session["UserType"].ToString() == "WIM_A")
                            {
                                return RedirectToAction("../User/Dashboard");
                            }
                            else
                            {
                                OrganizationSubscription OrgSubscription = _IOrganizationSubscriptionRepository.GetOrganizationSubscriptionByOrgID(new Guid(Session["OrganizationGUID"].ToString()));
                                if (OrgSubscription != null && OrgSubscription.IsActive == true)
                                {
                                    if (DateTime.Compare(Convert.ToDateTime(OrgSubscription.ExpiryDate), DateTime.UtcNow) >= 0)
                                    {
                                        UserSubscription userSubscription = _IUserSubscriptionRepository.GetUserSubscriptionByUserID(new Guid(Session["UserGUID"].ToString()));
                                        if (userSubscription != null && userSubscription.IsActive)
                                        {
                                            return RedirectToAction("Dashboard");
                                        }
                                        else
                                        {
                                            TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion - Subscription','User is not Subscribed');</script>";
                                            return RedirectToAction("Login");
                                        }
                                    }
                                    else
                                    {
                                        TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion - Subscription','Organization Subscription is Expired');</script>";
                                        return RedirectToAction("Login");
                                    }
                                }
                                else
                                {
                                    TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion - Subscription','Organization is not Subscribed');</script>";
                                    return RedirectToAction("Login");
                                }
                            }
                        }

                    }
                    else
                    {
                        TempData["msg"] = "<script>ModalPopupsAlert('Workers-In-Motion - Login error','Enter valid User Name and Password');</script>";
                        return RedirectToAction("Login");
                    }

                }
                return View(login);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return View(login);
            }
        }
        private int CreateMasterLogin(MasterLogin pMasterLogin, ref string UpdatedSessionID)
        {
            int lResult;
            IUserRepository _IUserRepository = new UserRepository(new WorkersInMotionDB());
            MasterLogin lMasterLogin = new MasterLogin();
            lMasterLogin.LoginGUID = Guid.NewGuid();
            lMasterLogin.LoginType = pMasterLogin.LoginType;
            lMasterLogin.UserGUID = pMasterLogin.UserGUID;
            lMasterLogin.IsActive = true;
            lMasterLogin.SessionID = Guid.NewGuid().ToString();
            UpdatedSessionID = lMasterLogin.SessionID.ToString();
            lMasterLogin.ExpiryTime = pMasterLogin.ExpiryTime;
            lMasterLogin.SessionTimeOut = 60;
            lMasterLogin.IsLoggedIn = true;
            lMasterLogin.Phone = "";
            lMasterLogin.CreateDate = DateTime.UtcNow;
            lMasterLogin.CreateBy = pMasterLogin.UserGUID;
            lMasterLogin.LastModifiedDate = DateTime.UtcNow;
            lMasterLogin.LastModifiedBy = pMasterLogin.UserGUID;
            //_IUserRepository.InsertMasterLogin(lMasterLogin);
            if (_IUserRepository.InsertMasterLogin(lMasterLogin) > 0)
            {
                lResult = 1;
            }
            else
            {
                lResult = 0;

            }
            return lResult;
        }
        //public MasterLogin GetMasterLoginByUserGUID(Guid UserGUID)
        //{
        //    using (var dataContext = new WorkersInMotionDB())
        //    {
        //        return (from p in dataContext.MasterLogins
        //                where p.UserGUID == UserGUID
        //                select p).FirstOrDefault();

        //    }
        //}
        public List<MasterLogin> GetMasterLogin(MasterLogin pMasterLogin)
        {
            //using (var dataContext = new WorkersInMotionDB())
            //{
            //    return (from p in dataContext.MasterLogins
            //            where p.UserGUID == pMasterLogin.UserGUID
            //            && (pMasterLogin.LoginType == null || p.LoginType == pMasterLogin.LoginType)
            //            select p).ToList();

            //}
            SqlParameter[] Param = new SqlParameter[2];
            Param[0] = new SqlParameter("@pUserGUID", SqlDbType.UniqueIdentifier);
            Param[0].Value = pMasterLogin.UserGUID;
            Param[1] = new SqlParameter("@pLoginType", SqlDbType.SmallInt);
            Param[1].Value = pMasterLogin.LoginType;

            return context.Database.SqlQuery<MasterLogin>("select * from  MasterLogins where UserGUID=@pUserGUID and (LoginType is null or LoginType=@pLoginType)", Param).ToList();

        }
        //public void UpdateMasterLogin(MasterLogin masterlogin)
        //{
        //    context.Entry(masterlogin).State = EntityState.Modified;
        //}
        public string UpdateMasterLogin(MasterLogin pMasterLogin)
        {
            try
            {

                //using (var dataContext = new WorkersInMotionDB())
                //{
                //    var qry = from p in dataContext.MasterLogins where p.LoginGUID == pMasterLogin.LoginGUID select p;
                //    var item = qry.Single();
                //    item.SessionID = Guid.NewGuid().ToString();
                //    item.LastModifiedDate = DateTime.UtcNow;
                //    item.LastModifiedBy = pMasterLogin.UserGUID;
                //    item.ExpiryTime = pMasterLogin.ExpiryTime;
                //    if (dataContext.SaveChanges() > 0)
                //    {
                //        return item.SessionID;
                //    }
                //    else
                //    {
                //        return string.Empty;
                //    }

                //}
                MasterLogin mLogin = GetMasterLoginByLoginID(pMasterLogin.LoginGUID);
                if (mLogin != null)
                {
                    mLogin.SessionID = Guid.NewGuid().ToString();
                    mLogin.LastModifiedDate = DateTime.UtcNow;
                    SqlParameter[] Param = new SqlParameter[5];
                    Param[0] = new SqlParameter("@pLoginGUID", SqlDbType.UniqueIdentifier);
                    Param[0].Value = mLogin.LoginGUID;
                    Param[1] = new SqlParameter("@pSessionID", SqlDbType.NVarChar, -1);
                    Param[1].Value = mLogin.SessionID;
                    Param[2] = new SqlParameter("@pLastModifiedDate", SqlDbType.DateTime);
                    Param[2].Value = mLogin.LastModifiedDate;
                    Param[3] = new SqlParameter("@pLastModifiedBy", SqlDbType.UniqueIdentifier);
                    Param[3].Value = pMasterLogin.UserGUID;
                    Param[4] = new SqlParameter("@pExpiryTime", SqlDbType.DateTime);
                    Param[4].Value = pMasterLogin.ExpiryTime;

                    int result = context.Database.ExecuteSqlCommand("update MasterLogins set SessionID=@pSessionID,LastModifiedDate=@pLastModifiedDate,LastModifiedBy=@pLastModifiedBy,ExpiryTime=@pExpiryTime where LoginGUID=@pLoginGUID", Param);
                    if (result > 0)
                    {
                        return mLogin.SessionID;
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                else
                    return string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public int UpdateMasterLogins(MasterLogin masterLogin)
        {
            SqlParameter[] Param = new SqlParameter[3];
            Param[0] = new SqlParameter("@pLoginGUID", SqlDbType.UniqueIdentifier);
            Param[0].Value = masterLogin.LoginGUID;
            Param[1] = new SqlParameter("@pSessionID", SqlDbType.NVarChar, -1);
            Param[1].Value = masterLogin.SessionID;
            Param[2] = new SqlParameter("@pLastModifiedDate", SqlDbType.DateTime);
            Param[2].Value = masterLogin.LastModifiedDate;

            return context.Database.ExecuteSqlCommand("update MasterLogins set SessionID=@pSessionID,LastModifiedDate=@pLastModifiedDate where LoginGUID=@pLoginGUID", Param);

        }
        public MasterLogin UserLoginServer(string userName, string password)
        {
            Logger.Debug("Inside UserLogin");
            MasterLogin masterlogin = new MasterLogin();
            try
            {
                //  MasterLogin masterlogin = new MasterLogin();
                //using (var dataContext = new WorkersInMotionDB())
                //{
                //    var aspuser = (from p in dataContext.GlobalUsers
                //                   where p.UserName == userName && p.Password == password
                //                   select p).FirstOrDefault();
                //    if (aspuser != null)
                //    {
                //        Logger.Debug("Inside Role");
                //        AspNetRole asprole = GetRole(aspuser.Role_Id);

                //        masterlogin = (from p in dataContext.MasterLogins
                //                       where p.UserGUID == aspuser.UserGUID
                //                       select p).FirstOrDefault();
                //        if (masterlogin != null)
                //        {
                //            Logger.Debug("Inside MasterLogin");

                //            var qry = from p in dataContext.MasterLogins where p.LoginGUID == masterlogin.LoginGUID select p;
                //            var item = qry.Single();
                //            item.SessionID = Guid.NewGuid().ToString();
                //            item.LastModifiedDate = DateTime.UtcNow;
                //            item.LastModifiedBy = aspuser.UserGUID;
                //            if (dataContext.SaveChanges() > 0)
                //            {
                //                return masterlogin;
                //            }
                //            else
                //            {
                //                return null;
                //            }
                //        }
                //        else
                //        {
                //            Logger.Debug("Inside MasterLogin Create");
                //            masterlogin = new MasterLogin();
                //            masterlogin.LoginGUID = Guid.NewGuid();
                //            masterlogin.UserGUID = aspuser.UserGUID;
                //            masterlogin.IsActive = true;
                //            masterlogin.SessionID = Guid.NewGuid().ToString();
                //            masterlogin.ExpiryTime = DateTime.UtcNow;
                //            masterlogin.SessionTimeOut = 60;
                //            masterlogin.IsLoggedIn = true;
                //            masterlogin.Phone = "";
                //            masterlogin.CreateDate = DateTime.UtcNow;
                //            masterlogin.CreateBy = aspuser.UserGUID;
                //            masterlogin.LastModifiedDate = DateTime.UtcNow;
                //            masterlogin.LastModifiedBy = aspuser.UserGUID;
                //            int result = InsertMasterLogin(masterlogin);
                //            //int result = Save();
                //            if (result > 0)
                //            {
                //                return masterlogin;
                //            }
                //            else
                //            {
                //                return null;
                //            }

                //        }
                //    }
                //    else
                //    {
                //        Logger.Debug("AspUser is null");
                //        return null;
                //    }
                //}

                using (var dataContext = new WorkersInMotionDB())
                {

                    var aspuser = UserLogin(userName, password);
                    if (aspuser != null)
                    {
                        Logger.Debug("Inside Role");
                        AspNetRole asprole = GetRole(aspuser.Role_Id);

                        SqlParameter[] Param = new SqlParameter[1];
                        Param[0] = new SqlParameter("@pUserGUID", SqlDbType.UniqueIdentifier);
                        Param[0].Value = aspuser.UserGUID;

                        masterlogin = context.Database.SqlQuery<MasterLogin>("select * from  MasterLogins where UserGUID=@pUserGUID", Param).FirstOrDefault();

                        if (masterlogin != null)
                        {
                            Logger.Debug("Inside MasterLogin");

                            //var qry = from p in dataContext.MasterLogins where p.LoginGUID == masterlogin.LoginGUID select p;
                            //var item = qry.Single();
                            //item.SessionID = Guid.NewGuid().ToString();
                            //item.LastModifiedDate = DateTime.UtcNow;
                            //item.LastModifiedBy = aspuser.UserGUID;

                            masterlogin.SessionID = Guid.NewGuid().ToString();
                            masterlogin.LastModifiedDate = DateTime.UtcNow;
                            if (UpdateMasterLogins(masterlogin) > 0)
                            {
                                return masterlogin;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        else
                        {
                            Logger.Debug("Inside MasterLogin Create");
                            masterlogin = new MasterLogin();
                            masterlogin.LoginGUID = Guid.NewGuid();
                            masterlogin.UserGUID = aspuser.UserGUID;
                            masterlogin.IsActive = true;
                            masterlogin.SessionID = Guid.NewGuid().ToString();
                            masterlogin.ExpiryTime = DateTime.UtcNow;
                            masterlogin.SessionTimeOut = 60;
                            masterlogin.IsLoggedIn = true;
                            masterlogin.Phone = "";
                            masterlogin.CreateDate = DateTime.UtcNow;
                            masterlogin.CreateBy = aspuser.UserGUID;
                            masterlogin.LastModifiedDate = DateTime.UtcNow;
                            masterlogin.LastModifiedBy = aspuser.UserGUID;
                            int result = InsertMasterLogin(masterlogin);
                            //int result = Save();
                            if (result > 0)
                            {
                                return masterlogin;
                            }
                            else
                            {
                                return null;
                            }

                        }
                    }
                    else
                    {
                        Logger.Debug("AspUser is null");
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return null;
            }
        }
        public int InsertMasterLogin(MasterLogin masterlogin)
        {
            //context.MasterLogins.Add(masterlogin);
            SqlParameter[] Param = new SqlParameter[13];
            Param[0] = new SqlParameter("@pLoginGUID", SqlDbType.UniqueIdentifier);
            Param[0].Value = masterlogin.LoginGUID;
            Param[1] = new SqlParameter("@pLoginType", SqlDbType.SmallInt);
            Param[1].Value = (object)masterlogin.LoginType ?? DBNull.Value;
            Param[2] = new SqlParameter("@pUserGUID", SqlDbType.UniqueIdentifier);
            Param[2].Value = masterlogin.UserGUID;
            Param[3] = new SqlParameter("@pIsActive", SqlDbType.Bit);
            Param[3].Value = masterlogin.IsActive;
            Param[4] = new SqlParameter("@pSessionID", SqlDbType.NVarChar, -1);
            Param[4].Value = (object)masterlogin.SessionID ?? DBNull.Value;
            Param[5] = new SqlParameter("@pExpiryTime", SqlDbType.DateTime);
            Param[5].Value = masterlogin.ExpiryTime;
            Param[6] = new SqlParameter("@pSessionTimeOut", SqlDbType.Int);
            Param[6].Value = (object)masterlogin.SessionTimeOut ?? DBNull.Value;
            Param[7] = new SqlParameter("@pIsLoggedIn", SqlDbType.Bit);
            Param[7].Value = (object)masterlogin.IsLoggedIn ?? DBNull.Value;
            Param[8] = new SqlParameter("@pPhone", SqlDbType.NVarChar, -1);
            Param[8].Value = (object)masterlogin.Phone ?? DBNull.Value;
            Param[9] = new SqlParameter("@pCreateDate", SqlDbType.DateTime);
            Param[9].Value = (object)masterlogin.CreateDate ?? DBNull.Value;
            Param[10] = new SqlParameter("@pCreateBy", SqlDbType.UniqueIdentifier);
            Param[10].Value = (object)masterlogin.CreateBy ?? DBNull.Value;
            Param[11] = new SqlParameter("@pLastModifiedDate", SqlDbType.DateTime);
            Param[11].Value = (object)masterlogin.LastModifiedDate ?? DBNull.Value;
            Param[12] = new SqlParameter("@pLastModifiedBy", SqlDbType.UniqueIdentifier);
            Param[12].Value = (object)masterlogin.LastModifiedBy ?? DBNull.Value;

            return context.Database.ExecuteSqlCommand("insert into MasterLogins(LoginGUID,LoginType,UserGUID,IsActive,SessionID,ExpiryTime,SessionTimeOut,IsLoggedIn,Phone,CreateDate,CreateBy,LastModifiedDate,LastModifiedBy)"
                + "values(@pLoginGUID,@pLoginType,@pUserGUID,@pIsActive,@pSessionID,@pExpiryTime,@pSessionTimeOut,@pIsLoggedIn,@pPhone,@pCreateDate,@pCreateBy,@pLastModifiedDate,@pLastModifiedBy)", Param);
        }
Esempio n. 8
0
        public LoginResponse Login(LoginRequest pLoginRequest)
        {
            LoginResponse loginResponse = new LoginResponse();
            IUserRepository _IUserRepository;
            _IUserRepository = new UserRepository(new WorkersInMotionDB());
            string UpdatedSessionID = string.Empty;
            Logger.Debug("Inside UserLogin");
            LoginResponse lResponse = new LoginResponse();
            try
            {
                MasterLogin masterlogin = new MasterLogin();
                var aspuser = _IUserRepository.UserLogin(pLoginRequest.UserName, _IUserRepository.EncodeTo64(pLoginRequest.Password));
                if (aspuser != null)
                {
                    Logger.Debug("Inside Role");
                    AspNetRole asprole = _IUserRepository.GetRole(aspuser.Role_Id);
                    switch (asprole.UserType)
                    {
                        case "WIM_A":
                        case "ENT_A":
                        case "ENT_OM":
                        case "ENT_U_RM":
                        case "ENT_U_TM":
                            lResponse.Role = 1;
                            break;
                        case "ENT_U":
                            lResponse.Role = 2;
                            break;
                        case "IND_C":
                            lResponse.Role = 3;
                            break;
                        default:
                            break;
                    }

                    UserDevice userDevice = new UserDevice();
                    List<MasterLogin> masterlogins = new List<MasterLogin>();
                    MasterLogin lMasterLogin = new MasterLogin();
                    lMasterLogin.UserGUID = aspuser.UserGUID;
                    lMasterLogin.LoginType = (short)pLoginRequest.LoginType;
                    masterlogins = _IUserRepository.GetMasterLogin(lMasterLogin);
                    if (masterlogins != null && masterlogins.Count > 0)
                    {
                        #region masterlogins record available
                        masterlogin = masterlogins[0]; // Alok need to be fixed
                        // Update the Master Login
                        masterlogin.ExpiryTime = DateTime.UtcNow.AddYears(10);
                        Logger.Debug("Updating MasterLogin Record");
                        UpdatedSessionID = _IUserRepository.UpdateMasterLogin(masterlogin);
                        if (!string.IsNullOrEmpty(UpdatedSessionID))
                        {
                            #region UpdatedSessionID is not null

                            Logger.Debug("Updated Session ID: " + UpdatedSessionID);
                            lResponse.SessionID = UpdatedSessionID;
                            lResponse.UserGUID = aspuser.UserGUID.ToString();

                            Logger.Debug("Inside MasterLogin");
                            userDevice.LoginGUID = masterlogin.LoginGUID;
                            userDevice.DeviceID = pLoginRequest.DeviceInfo.deviceid;
                            List<UserDevice> lUserDevices = _IUserRepository.GetUserDevice(userDevice);
                            if (lUserDevices != null && lUserDevices.Count > 0)
                            {
                                // Delete the user device record
                                userDevice = lUserDevices[0]; // Need to modify Alok
                                int deviceresult = _IUserRepository.DeleteUserDevices(userDevice.UserDevicesGUID);
                                //int deviceresult = _IUserRepository.Save();
                                if (deviceresult <= 0)
                                {
                                    lResponse = null;
                                    return lResponse;
                                }
                            }
                            // Insert the User Device info
                            if (CreateUserDevice(masterlogin, pLoginRequest) > 0)
                            {
                                if (!string.IsNullOrEmpty(lResponse.SessionID) && !string.IsNullOrEmpty(lResponse.UserGUID))
                                {
                                    DownloadUsers lDownloadUsers = DownloadUsers(lResponse.SessionID, new Guid(lResponse.UserGUID));
                                    if (lDownloadUsers != null && lDownloadUsers.UserRecords.Count > 0)
                                        lResponse.UserRecord = lDownloadUsers.UserRecords[0];
                                    else
                                        lResponse.UserRecord = null;
                                }
                                Logger.Debug("UserDevice record created for updated Session ID: " + UpdatedSessionID);

                            }
                            #endregion
                        }
                        else
                        {
                            #region UpdatedSessionID is NULL
                            Logger.Error("Unable to generate Session ID");
                            lResponse = null;
                            return lResponse;
                            #endregion
                        }
                        #endregion
                    }
                    else
                    {
                        #region masterlogins record not available
                        Logger.Debug("Creating MasterLogin Record");
                        lMasterLogin.ExpiryTime = DateTime.UtcNow.AddYears(10);
                        if (CreateMasterLogin(lMasterLogin) > 0)
                        {
                            Logger.Debug("New Session ID: " + lMasterLogin.SessionID);
                            lResponse.SessionID = lMasterLogin.SessionID;
                            lResponse.UserGUID = lMasterLogin.UserGUID.ToString();
                            Logger.Debug("Inside UserDevice create");

                            if (CreateUserDevice(masterlogin, pLoginRequest) > 0)
                            {
                                if (!string.IsNullOrEmpty(lResponse.SessionID) && !string.IsNullOrEmpty(lResponse.UserGUID))
                                {
                                    DownloadUsers lDownloadUsers = DownloadUsers(lResponse.SessionID, new Guid(lResponse.UserGUID));
                                    if (lDownloadUsers != null && lDownloadUsers.UserRecords.Count > 0)
                                        lResponse.UserRecord = lDownloadUsers.UserRecords[0];
                                    else
                                        lResponse.UserRecord = null;
                                }
                                Logger.Debug("UserDevice record created for new Session ID: " + lMasterLogin.SessionID);
                            }
                            else
                            {
                                Logger.Error("Unable to craete UserDevice record for new Session ID: " + lMasterLogin.SessionID);
                                lResponse = null;
                            }
                        }
                        else
                        {
                            Logger.Error("Unable to craete MasterLogin record");
                            lResponse = null;
                        }
                        #endregion
                    }
                }
                else
                {
                    Logger.Error("Unable to find user record in  AspUser");
                    lResponse = null;
                }
                return lResponse;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                lResponse = null;
                //lResponse.SessionID = new WorkersInMotionDB().Database.Connection.ConnectionString + " Exception: " + ex.Message;
                return lResponse;
            }

        }
Esempio n. 9
0
        private int CreateUserDevice(MasterLogin pMasterLogin, LoginRequest pLoginRequest)
        {
            int lResult;
            IUserRepository _IUserRepository = new UserRepository(new WorkersInMotionDB());
            UserDevice lUserDevice = new UserDevice();
            lUserDevice.UserDevicesGUID = Guid.NewGuid();
            lUserDevice.LoginGUID = pMasterLogin.LoginGUID;
            lUserDevice.UserGUID = pMasterLogin.UserGUID;
            lUserDevice.IPAddress = pLoginRequest.DeviceInfo.deviceipaddress;
            lUserDevice.DeviceID = pLoginRequest.DeviceInfo.deviceid;
            lUserDevice.DeviceInfo = new JavaScriptSerializer().Serialize(pLoginRequest.DeviceInfo);
            lUserDevice.DeviceType = pLoginRequest.DeviceInfo.devicetype;
            lUserDevice.PUSHID = pLoginRequest.PushID;
            lUserDevice.Phone = pMasterLogin.Phone;
            lUserDevice.IsActive = true;
            lUserDevice.TimeZone = pLoginRequest.DeviceInfo.TimeZone;
            lUserDevice.CreateDate = DateTime.UtcNow;
            lUserDevice.CreateBy = pMasterLogin.UserGUID;
            lUserDevice.LastModifiedDate = DateTime.UtcNow;
            lUserDevice.LastModifiedBy = pMasterLogin.UserGUID;
            // _IUserRepository.InsertUserDevice(lUserDevice);
            if (_IUserRepository.InsertUserDevice(lUserDevice) > 0)
            {
                lResult = 1;
            }
            else
            {
                lResult = 0;

            }
            return lResult;
        }