Ejemplo n.º 1
0
        /// <summary>
        /// Reads cookies from ASProxy cookies and adds them to request to site
        /// </summary>
        /// <param name="httpRequest">Proxy request</param>
        public override void AddCookiesToRequest(WebRequest httpRequest)
        {
            // ---------------------------------------
            // Declarations
            if (!(httpRequest is HttpWebRequest) || HttpContext.Current == null)
            {
                return;
            }

            HttpResponse userResponse = HttpContext.Current.Response;
            HttpRequest  userRequest  = HttpContext.Current.Request;

            if (userRequest == null || userResponse == null)
            {
                return;
            }
            HttpWebRequest webRequest = (HttpWebRequest)httpRequest;

            string hostName = webRequest.Address.Host;
            Uri    webUri   = webRequest.Address;

            // ---------------------------------------
            // The code

            // Enable the cookies!
            if (webRequest.CookieContainer == null)
            {
                webRequest.CookieContainer = new CookieContainer();
            }

            // Get cookies to be serached
            // The cookie names that cab be applied according to host name
            List <string> cookiesName = Internal.GetCookiesNameToBeSearched(hostName);

            for (int i = 0; i < cookiesName.Count; i++)
            {
                string cookieName = cookiesName[i];

                // Get cookie from user request
                HttpCookie userCookie = userRequest.Cookies[cookieName];

                // If there is no cookie
                if (userCookie == null)
                {
                    continue;
                }

                // Decode requested cookie value,
                userCookie.Value = HttpUtility.UrlDecode(userCookie.Value);

                // Apply cookie to site request
                Internal.ApplyCookiesToContainerByHeader(webRequest.CookieContainer, webUri, userCookie.Value);
            }
        }