/// <summary> /// Create new session during login /// </summary> string IUserManager.CreateSession(CreateNewSession SessionModel) { var found = SessionModel; if (found != null) { //Context.UserLoginSessions.Where(p => p.UserId == model.UserId).ToList().ForEach(p => { p.SessionExpired = true; }); Context.UserLoginSessions.Where(p => p.UserId == SessionModel.UserID).ToList().ForEach(p => { p.SessionExpired = true; p.DeviceToken = null; p.LoggedOutTime = DateTime.UtcNow; p.IsActive = false; }); //Context.UserLoginSessions.Where(p => p.UniqueDeviceId == SessionModel.UniqueDeviceId).ToList().ForEach(p => { p.SessionExpired = true; p.DeviceToken = null; p.LoggedOutTime = DateTime.UtcNow; p.IsActive = false; }); Context.SaveChanges(); var session = new UserLoginSession() { LoggedInTime = DateTime.Now, SessionExpired = false, UserId = found.UserID, UserLoginSessionID = Guid.NewGuid(), UniqueDeviceId = SessionModel.UniqueDeviceId, DeviceToken = SessionModel.DeviceToken, DeviceType = SessionModel.DeviceType, LastActivityTime = DateTime.UtcNow, IsActive = true }; Context.UserLoginSessions.Add(session); Context.SaveChanges(); return(session.UserLoginSessionID.ToString()); } else { return(string.Empty); } }
/// This method is used to validate the user session id /// </summary> UserModel IUserManager.ValidateUserSession(CreateNewSession SessionModel) { var session = Context.UserLoginSessions.Where(o => !o.SessionExpired && o.LoggedOutTime == null && o.UniqueDeviceId.Equals(SessionModel.UniqueDeviceId) && SessionModel.sessionId == o.UserLoginSessionID && o.UserTbl.Status != (int)UserStatuss.Deleted && o.UserTbl.Status != (int)UserStatuss.Blocked).FirstOrDefault(); if (session != null) { session.IsActive = true; session.LastActivityTime = DateTime.UtcNow; session.UniqueDeviceId = SessionModel.UniqueDeviceId; session.DeviceToken = SessionModel.DeviceToken; session.DeviceType = SessionModel.DeviceType; session.LoggedOutTime = null; session.TokenVOIP = SessionModel.TokenVOIP; Context.SaveChanges(); } if (session != null) { return(new UserModel(session.UserLoginSessionID, session.UserTbl)); } else { return(null); } }
/// <summary> /// create new session when user trying to login /// </summary> /// <param name="userId"></param> /// <returns></returns> public string CreateNewSession(int userId) { CreateNewSession SessionModel = new CreateNewSession(); SessionModel.UserID = userId; SessionModel.UniqueDeviceId = Request.Headers.GetValues("UniqueDeviceId").FirstOrDefault(); SessionModel.DeviceToken = Request.Headers.GetValues("DeviceID").FirstOrDefault(); SessionModel.DeviceType = Int32.Parse(Request.Headers.GetValues("DeviceType").FirstOrDefault()); var token = _userManager.CreateSession(SessionModel); return(token); }
public override void OnAuthorization(HttpActionContext actionContext) { try { if ((actionContext.Request.Method != HttpMethod.Post && actionContext.Request.Method != HttpMethod.Get)) { actionContext.Response = new JsonContent("Only POST and GET requests are allowed.", Status.Failed).ConvertToHttpResponseOK(); } var baseController = (BaseAPIController)actionContext.ControllerContext.Controller; var skipAuthorization = actionContext.ActionDescriptor.GetCustomAttributes <SkipAuthorization>().Any(); var skipAuthentication = actionContext.ActionDescriptor.GetCustomAttributes <SkipAuthentication>().Any(); var sessionToken = actionContext.Request.Headers.Any(m => m.Key == " ") ? actionContext.Request.Headers.GetValues("SessionId").FirstOrDefault() : null; var UniqueDeviceId = actionContext.Request.Headers.Any(m => m.Key == "UniqueDeviceId") ? actionContext.Request.Headers.GetValues("UniqueDeviceId").FirstOrDefault() : null; if (!skipAuthentication) { var secretKey = Config.ApniMaaSecretKey; var clientHash = actionContext.Request.Headers.GetValues("ClientHash").FirstOrDefault(); var timeStamp = actionContext.Request.Headers.GetValues("TimeStamp").FirstOrDefault(); if (sessionToken == null) { var validationHash = CommonMethods.HashCode(string.Format("{0}{1}", timeStamp, secretKey)); if (!validationHash.Equals(clientHash, StringComparison.InvariantCultureIgnoreCase)) { actionContext.Response = new JsonContent("Request could not be authenticated. Invalid hash encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } } else { var validationHash = CommonMethods.HashCode(string.Format("{0}{1}{2}", sessionToken, timeStamp, secretKey)); if (!validationHash.Equals(clientHash, StringComparison.InvariantCultureIgnoreCase)) { actionContext.Response = new JsonContent("Request could not be authenticated. Invalid hash encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } } } if (!skipAuthorization) { var deviceID = actionContext.Request.Headers.Any(m => m.Key == "DeviceID") ? actionContext.Request.Headers.GetValues("DeviceID").FirstOrDefault() : null; if (deviceID == null) { actionContext.Response = new JsonContent("Request could not be authorized. Invalid deviceID encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } if (UniqueDeviceId == null) { actionContext.Response = new JsonContent("Request could not be authorized. Invalid Unique Device ID encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } CreateNewSession SessionModel = new CreateNewSession(); var sessionId = new Guid(sessionToken); SessionModel.UniqueDeviceId = UniqueDeviceId; SessionModel.sessionId = sessionId; SessionModel.DeviceToken = actionContext.Request.Headers.GetValues("DeviceID").FirstOrDefault(); SessionModel.DeviceType = Int32.Parse(actionContext.Request.Headers.GetValues("DeviceType").FirstOrDefault()); var deviceTypeID = actionContext.Request.Headers.Any(m => m.Key == "DeviceType") ? actionContext.Request.Headers.GetValues("DeviceType").FirstOrDefault() : "-1"; if (deviceTypeID == null) { actionContext.Response = new JsonContent("Request could not be authorized. Invalid Device type encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } else { int deviceType = Convert.ToInt32(deviceTypeID); if ((int)RegisterVia.IPhone == deviceType) { //chages on 22/10/2018 for iphone //SessionModel.TokenVOIP = actionContext.Request.Headers.GetValues("TokenVOIP").FirstOrDefault(); } } UserModel loginSession = _userManager.ValidateUserSession(SessionModel); if (loginSession == null) { actionContext.Response = new JsonContent("Your session has expired.Please Log in again to continue.", Status.SessionExpired).ConvertToHttpResponseOK(); return; } else { if (deviceID == null) { actionContext.Response = new JsonContent("Request could not be authorized. Invalid deviceID encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } else { loginSession.DeviceId = deviceID; } deviceTypeID = actionContext.Request.Headers.Any(m => m.Key == "DeviceType") ? actionContext.Request.Headers.GetValues("DeviceType").FirstOrDefault() : "-1"; int deviceType = Convert.ToInt32(deviceTypeID); if ((int)RegisterVia.Android == deviceType || (int)RegisterVia.IPhone == deviceType) { loginSession.DeviceType = deviceType; } else { actionContext.Response = new JsonContent("Request could not be authorized. Invalid DeviceType encountered!", Status.Failed).ConvertToHttpResponseOK(); return; } if (loginSession == null) { actionContext.Response = new JsonContent("Your session has expired.Please Log in again to continue.", Status.SessionExpired).ConvertToHttpResponseOK(); return; } else { baseController.LOGGED_IN_USER = new UserModel { UserID = loginSession.UserID, Email = loginSession.Email, SessionId = sessionId, //UsertypeId = loginSession.UsertypeId, //SeekingFor = loginSession.SeekingFor, //Birthday = loginSession.Birthday, //Gender = loginSession.Gender, //Location = loginSession.Location, //userName = loginSession.userName, //DateCreated = loginSession.DateCreated, //DateModified = loginSession.DateModified, //IsDeleted = loginSession.IsDeleted, //lookingType = loginSession.lookingType, //Sexuality = loginSession.Sexuality, DeviceType = loginSession.DeviceType, DeviceId = loginSession.DeviceId, UniqueDeviceId = UniqueDeviceId, //ProfileImage = loginSession.ProfileImage != null ? loginSession.ProfileImage : Config.DomainUrl + AppDefaults.ProfilePicDirectory + AppDefaults.DummyUserImage }; } } } } catch (Exception ex) { System.IO.StreamWriter sw = null; try { sw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/ErrorLog.txt"), true); sw.WriteLine(ex.Message); sw.WriteLine("http://jsonformat.com/"); sw.WriteLine(ex); sw.WriteLine(""); sw.WriteLine(""); } catch { } finally { sw.Close(); } } }