コード例 #1
0
        private CookieHeaderValue(CookieHeaderValue source)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }

            this.Expires = source.Expires;
            this.MaxAge = source.MaxAge;
            this.Domain = source.Domain;
            this.Path = source.Path;
            this.Secure = source.Secure;
            this.HttpOnly = source.HttpOnly;

            foreach (CookieState cookie in source.Cookies)
            {
                this.Cookies.Add(cookie.Clone<CookieState>());
            }
        }
コード例 #2
0
        private static bool ParseCookieSegment(CookieHeaderValue instance, string segment)
        {
            if (String.IsNullOrWhiteSpace(segment))
            {
                return true;
            }

            string[] nameValue = segment.Split(nameValueSeparator, 2);
            if (nameValue.Length < 1 || String.IsNullOrWhiteSpace(nameValue[0]))
            {
                return false;
            }

            string name = nameValue[0].Trim();
            if (String.Equals(name, ExpiresToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                DateTimeOffset expires;
                if (FormattingUtilities.TryParseDate(value, out expires))
                {
                    instance.Expires = expires;
                    return true;
                }
                return false;
            }
            else if (String.Equals(name, MaxAgeToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                int maxAge;
                if (FormattingUtilities.TryParseInt32(value, out maxAge))
                {
                    instance.MaxAge = new TimeSpan(0, 0, maxAge);
                    return true;
                }
                return false;
            }
            else if (String.Equals(name, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                instance.Domain = GetSegmentValue(nameValue, null);
                return true;
            }
            else if (String.Equals(name, PathToken, StringComparison.OrdinalIgnoreCase))
            {
                instance.Path = GetSegmentValue(nameValue, DefaultPath);
                return true;
            }
            else if (String.Equals(name, SecureToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!String.IsNullOrWhiteSpace(value))
                {
                    return false;
                }
                instance.Secure = true;
                return true;
            }
            else if (String.Equals(name, HttpOnlyToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!String.IsNullOrWhiteSpace(value))
                {
                    return false;
                }
                instance.HttpOnly = true;
                return true;
            }
            else
            {
                string value = GetSegmentValue(nameValue, null);

                // We read the cookie segment as form data
                try
                {
                    FormDataCollection formData = new FormDataCollection(value);
                    NameValueCollection values = formData.ReadAsNameValueCollection();
                    CookieState cookie = new CookieState(name, values);
                    instance.Cookies.Add(cookie);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
コード例 #3
0
        public static bool TryParse(string input, out CookieHeaderValue parsedValue)
        {
            parsedValue = null;
            if (!String.IsNullOrEmpty(input))
            {
                string[] segments = input.Split(segmentSeparator);
                CookieHeaderValue instance = new CookieHeaderValue();
                foreach (string segment in segments)
                {
                    if (!ParseCookieSegment(instance, segment))
                    {
                        return false;
                    }
                }

                // If we didn't find any cookie state name/value pairs then cookie is not valid
                if (instance.Cookies.Count == 0)
                {
                    return false;
                }

                parsedValue = instance;
                return true;
            }

            return false;
        }