Class used to record details of devices that have already accessed the mobile web site to determine if the request is the 1st one or a subsequent one.
Наследование: IRequestHistory
Пример #1
0
        /// <summary>
        /// Records in the session (if present) and in a cookie
        /// the requesting devices first request. This ensures subsequent calls
        /// to IsFirstTime return the correct value.
        /// </summary>
        /// <param name="context">The context of the request.</param>
        private static void RecordFirstTime(HttpContext context)
        {
            // Add a parameter to the session if available indicating the time that
            // the device date should be remvoed from the session.
            if (context.Session != null)
            {
                SetSession(context);
            }

            // Add a cookie to the response setting the expiry time to the
            // redirection timeout.
            // Modified to check for existance of cookie to avoid recreating.
            SetResponseCookie(context, new HttpCookie(Constants.AlreadyAccessedCookieName));

            // Add to the request history cache.
            RequestHistory.Add(context.Request);
        }
Пример #2
0
        /// <summary>
        /// Determines if this is the first request received from the device.
        /// </summary>
        /// <param name="context">Context of the request.</param>
        /// <returns>True if this request is the first from the device. Otherwise false.</returns>
        public static bool IsFirstTime(HttpContext context)
        {
            // If the parameter indicating only the first request should be redirect
            // is false then return true as the implication is all requests should
            // be redirected.
            if (_firstRequestOnly == false)
            {
                return(true);
            }

            // Check to see if the Referrer URL contains the same host name
            // as the current page request. If there is a match this request has
            // come from another web page on the same host and is not the 1st request.
            // The logic is only applied if an infinite timeout value is provided
            // because using this method there is no way of knowing when the referrer
            // url details were populated.
            if (_redirectTimeout == 0 &&
                context.Request.UrlReferrer != null &&
                context.Request.UrlReferrer.Host == context.Request.Url.Host)
            {
                // In some situations the same web application may be split across
                // different host names. Record the first time details using other
                // methods to ensure this method returns the correct value when
                // used with subsequent requests from the same device.
                RecordFirstTime(context);
                return(false);
            }

            // If the session is available and it's timeout is greater then or equal to
            // the timeout to be used for redirection check to see if we have a
            // session parameter indicating an expiry time for the current device.
            // If the expiry time has not elpased then reset it and return a value
            // indicating this is not the first time the device has been seen.
            if (_redirectTimeout != 0 &&
                context.Session != null &&
                context.Session.Timeout >= _redirectTimeout &&
                context.Session[Constants.ExpiryTime] != null &&
                (long)context.Session[Constants.ExpiryTime] >= DateTime.UtcNow.Ticks)
            {
                // Update the session key to indicate a new expiry time.
                SetSession(context);

                // Remove our own cookie from the response as it's not
                // needed because the session is working.
                if (context.Request.Cookies[Constants.AlreadyAccessedCookieName] != null)
                {
                    WipeResponseCookie(context, context.Request.Cookies[Constants.AlreadyAccessedCookieName]);
                }

                // Remove from the devices cache file as session can be used.
                RequestHistory.Remove(context.Request);

                return(false);
            }

            // Check to see if our cookie is present from a previous request and that
            // the expiry time is not passed. If it is present ensure it's expiry time
            // is updated in the response.
            HttpCookie alreadyAccessed = context.Request.Cookies[Constants.AlreadyAccessedCookieName];

            if (alreadyAccessed != null &&
                long.Parse(alreadyAccessed.Value) >= DateTime.UtcNow.Ticks)
            {
                SetResponseCookie(context, alreadyAccessed);

                // Remove from the devices cache file as cookie can be used.
                RequestHistory.Remove(context.Request);

                return(false);
            }

            // Check to see if the requested IP address and HTTP headers hashcode is
            // on record as having been seen before.
            if (RequestHistory.IsPresent(context.Request))
            {
                // Update the cache and other methods in case they can
                // be used in the future.
                RecordFirstTime(context);
                return(false);
            }

            // The url referrer, session and cookie checks have all failed.
            // Record the device information using the session if available, a cookie and the
            // request history cache file.
            RecordFirstTime(context);

            return(true);
        }