private string GetSession(string[] userData, bool isPopupOpened = false) { var userId = Convert.ToInt64(userData[1]); var user = _userRepository.GetUser(userId); var userName = user.UserLogin.UserName; FormsAuthentication.SetAuthCookie(userName, true); _sessionContext.UserSession = _loginService.GetUserSessionModel(userName); if (_sessionContext.UserSession.CurrentOrganizationRole == null) { throw new Exception("Your default role has been removed. Please contact your administrator."); } //Code Removed as it was redundant hit to Database //var loggedInUser = _userRepository.GetUser(userId); //_sessionContext.UserSession = _loginService.GetUserSessionModel(userId); //_loginOtpService.ResetOtp(loggedInUser.Id); _sessionContext.LastLoggedInTime = user.UserLogin.LastLogged.ToString(); _loginRepository.UpdateLoginStatus(_sessionContext.UserSession.UserId, true); var browserName = Request.Browser.Browser + " " + Request.Browser.Version; string sessionId = ""; var loginLog = new UserLoginLog(); var roles = _roleRepository.GetRolesByAlias(userData[0]); roles = roles.Where(x => x.ParentId == null); var role = roles.First(); var availableRole = _sessionContext.UserSession.AvailableOrganizationRoles.FirstOrDefault(x => x.RoleId == role.Id); if (availableRole == null) { throw new Exception("Your role is not available in HIP. Please contact your administrator."); } if (!isPopupOpened) { loginLog = _loginService.GetLatestUserLogin(userId); if (loginLog == null) //if no login present then we'll login the user in case of NON-POPUP request logins { sessionId = RegenrateSessionId(); MedicareLoginTask(user, sessionId, browserName, role, availableRole); } else { _sessionContext.UserSession.UserLoginLogId = loginLog.Id; sessionId = loginLog.BrowserSession; //if old login is found then use old session id too } } else { sessionId = RegenrateSessionId(); MedicareLoginTask(user, sessionId, browserName, role, availableRole); } // send the EHR Session to Medicare var token = (sessionId + "_" + _sessionContext.UserSession.UserId + "_" + _sessionContext.UserSession.CurrentOrganizationRole.RoleId + "_" + _sessionContext.UserSession.CurrentOrganizationRole.OrganizationId).Encrypt(); var auth = new MedicareUpdateTokenModel { Token = token, SessionId = sessionId }; if (_settings.SyncWithHra) { _medicareApiService.Post <string>(_settings.MedicareApiUrl + MedicareApiUrl.SetEhrToken, auth); } return(JsonConvert.SerializeObject(auth)); }