예제 #1
0
        /// <summary>
        /// Adds the search terms and results to the session.
        /// </summary>
        /// <param name="session">The session the method works on</param>
        /// <param name="cache">The cache to store the results in</param>
        /// <param name="searchTerms">A string of search terms.</param>
        /// <param name="searchResults">A dataTable of search results.</param>
        public static void AddSearchTermsAndResults(this ISession session, IMemoryCache cache, string searchTerms, List <SearchResultRow> searchResults)
        {
            var resultsToCache = new SearchResultsWrapper()
            {
                SearchResults = searchResults, SearchTerms = searchTerms
            };
            var key = Guid.NewGuid().ToString();

            // cache the results for 10 minutes.
            cache.Set(key, resultsToCache, new TimeSpan(0, ApplicationAdapter.GetMaxNumberOfMinutesToCacheSearchResults(), 0));

            // cache the key in the session so we can find back the actual results in the cache!
            session.SetString(SessionKeys.SearchResultsKey, key);
        }
예제 #2
0
        private static async Task LogOutIfNeeded(HttpContext context)
        {
            if (context.User.Identity.IsAuthenticated)
            {
                string nickName = context.User.Identity.Name;

                // if the user has to be logged out by force, do that now
                if (ApplicationAdapter.UserHasToBeLoggedOutByForce(nickName))
                {
                    await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

                    context.Session.Clear();
                    context.Response.Redirect(ApplicationAdapter.GetVirtualRoot());
                    ApplicationAdapter.RemoveUserFromListToBeLoggedOutByForce(nickName);
                }
            }
        }
예제 #3
0
        private static async Task RedirectToInitIfRequired(HttpContext context)
        {
            // check if there's an anonymous user in the database
            var anonymous = await UserGuiHelper.GetUserAsync(0);             // use hardcoded 0 id. This also makes sure a misconfigured db isn't used further.

            if (anonymous == null)
            {
                // database is empty
                context.Request.Path = ApplicationAdapter.GetVirtualRoot() + "Admin/Init";
            }
            else
            {
                if (anonymous.NickName != "Anonymous")
                {
                    // Misconfigured.
                    context.Request.Path = ApplicationAdapter.GetVirtualRoot() + "Error/1337";
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Initializes the session with the initial static data for the user.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="context"></param>
        public static async Task InitializeAsync(this ISession session, HttpContext context)
        {
            if (session.GetInt32(SessionKeys.SessionInitialized) == 1)
            {
                // already initialized
                return;
            }

            bool       useEntityBasedLastVisitDateTracking = false;
            UserEntity user = null;

            if (context.User.Identity.IsAuthenticated)
            {
                user = await UserGuiHelper.GetUserAsync(context.User.Identity.Name);

                if (user == null)
                {
                    user = await UserGuiHelper.GetUserAsync(0);                     // 0 is UserID of Anonymous Coward;
                }
                else
                {
                    // if the lastvisited date is null in the user entity, we'll use the cookie approach first
                    useEntityBasedLastVisitDateTracking = user.LastVisitedDate.HasValue;
                }
            }
            else
            {
                user = await UserGuiHelper.GetUserAsync(0);                 // 0 is UserID of Anonymous Coward
            }

            if (user == null || user.IsBanned)
            {
                // banned user, revert to AC
                user = await UserGuiHelper.GetUserAsync(0);

                useEntityBasedLastVisitDateTracking = false;
            }

            if (user == null || user.UserID <= 0)
            {
                await session.LoadAnonymousSessionDataAsync();
            }
            else
            {
                await session.LoadUserSessionDataAsync(user);
            }

            bool     isLastVisitDateValid    = false;
            DateTime lastVisitDate           = DateTime.Now;
            string   lastVisitDateCookieName = ApplicationAdapter.GetSiteName() + " LastVisitDate";

            // the last visited date is either stored in a cookie or on the server. Older versions of this forum system used cookie based last visited date storage,
            // newer versions use server side storage in the User entity. For non-logged in users, cookie based storage is still used.
            if (useEntityBasedLastVisitDateTracking)
            {
                lastVisitDate        = user.LastVisitedDate.Value;
                isLastVisitDateValid = true;
            }
            else
            {
                // read last visit date from cookie collection sent
                if (context.Request.Cookies[lastVisitDateCookieName] != null)
                {
                    string lastVisitDateAsString = context.Request.Cookies[lastVisitDateCookieName];

                    // convert to datetime
                    lastVisitDate = new DateTime(
                        int.Parse(lastVisitDateAsString.Substring(4, 4)),                            // Year
                        int.Parse(lastVisitDateAsString.Substring(2, 2)),                            // Month
                        int.Parse(lastVisitDateAsString.Substring(0, 2)),                            // Day
                        int.Parse(lastVisitDateAsString.Substring(8, 2)),                            // Hour
                        int.Parse(lastVisitDateAsString.Substring(10, 2)),                           // Minute
                        0);                                                                          // Seconds

                    isLastVisitDateValid = true;
                }
                else
                {
                    lastVisitDate = DateTime.Now;
                }
            }

            if (isLastVisitDateValid)
            {
                // store in session object
                session.AddLastVisitDate(lastVisitDate);
            }

            // update date
            if (useEntityBasedLastVisitDateTracking || (user != null && user.UserID != 0 && !user.LastVisitedDate.HasValue))
            {
                await UserManager.UpdateLastVisitDateForUserAsync(user.UserID);
            }

            // always write new cookie
            // cookie path is set to '/', to avoid path name casing mismatches. The cookie has a unique name anyway.
            context.Response.Cookies.Append(lastVisitDateCookieName, DateTime.Now.ToString("ddMMyyyyHHmm"),
                                            new CookieOptions()
            {
                Expires  = new DateTimeOffset(DateTime.Now.AddYears(1)),
                Path     = "/",
                SameSite = SameSiteMode.Lax,
                HttpOnly = true                                                                                 // no js accessibility
            });

            if (session.CheckIfNeedsAuditing(AuditActions.AuditLogin))
            {
                await SecurityManager.AuditLoginAsync(session.GetUserID());
            }

            // mark the session as initialized.
            session.SetInt32(SessionKeys.SessionInitialized, 1);
        }
예제 #5
0
        /// <summary>
        /// Gets the user preference DefaultNumberOfMessagesPerPage for the current user
        /// </summary>
        /// <param name="session">The session the method works on</param>
        /// <returns>the default # of messages per page as set by this user.</returns>
        public static int GetUserDefaultNumberOfMessagesPerPage(this ISession session)
        {
            int toReturn = session.GetInt32(SessionKeys.DefaultNumberOfMessagesPerPage) ?? 0;

            return(toReturn <= 0 ? ApplicationAdapter.GetMaxAmountMessagesPerPage() : toReturn);
        }