public void ProcessUserRequest(User user, HttpContextBase context)
        {
            if (context.Response.IsRequestBeingRedirected)
            {
                return;
            }
            int?userID = null;

            if (user != null)
            {
                userID = user.UserID;
            }
            if (context.Request.Cookies[_sessionIDCookieName] == null)
            {
                StartNewSession(context, userID);
                if (user != null)
                {
                    _userRepository.UpdateLastActivityDate(user, DateTime.UtcNow);
                }
            }
            else
            {
                var sessionID = Convert.ToInt32(context.Request.Cookies[_sessionIDCookieName].Value);
                if (user != null)
                {
                    _userRepository.UpdateLastActivityDate(user, DateTime.UtcNow);
                }
                var updateSuccess = _userSessionRepository.UpdateSession(sessionID, DateTime.UtcNow);
                if (!updateSuccess)
                {
                    StartNewSession(context, userID);
                }
                else
                {
                    var isAnon = _userSessionRepository.IsSessionAnonymous(sessionID);
                    if (userID.HasValue && isAnon || !userID.HasValue && !isAnon)
                    {
                        EndAndDeleteSession(new ExpiredUserSession {
                            UserID = null, SessionID = sessionID, LastTime = DateTime.UtcNow
                        });
                        StartNewSession(context, userID);
                    }
                }
            }
        }
        public async Task <int> ProcessUserRequest(User user, int?sessionID, string ip, Action deleteSession, Action <int> createSession)
        {
            int?userID = null;

            if (user != null)
            {
                userID = user.UserID;
            }
            if (sessionID == null)
            {
                sessionID = await StartNewSession(userID, ip, createSession);

                if (user != null)
                {
                    await _userRepository.UpdateLastActivityDate(user, DateTime.UtcNow);
                }
            }
            else
            {
                if (user != null)
                {
                    await _userRepository.UpdateLastActivityDate(user, DateTime.UtcNow);
                }
                var updateSuccess = await _userSessionRepository.UpdateSession(sessionID.Value, DateTime.UtcNow);

                if (!updateSuccess)
                {
                    sessionID = await StartNewSession(userID, ip, createSession);
                }
                else
                {
                    var isAnon = await _userSessionRepository.IsSessionAnonymous(sessionID.Value);

                    if (userID.HasValue && isAnon || !userID.HasValue && !isAnon)
                    {
                        deleteSession();
                        await EndAndDeleteSession(new ExpiredUserSession { UserID = null, SessionID = sessionID.Value, LastTime = DateTime.UtcNow });

                        sessionID = await StartNewSession(userID, ip, createSession);
                    }
                }
            }
            return(sessionID.Value);
        }