Beispiel #1
0
        static HttpCookie GetAspxAuthCookie(HttpResponseMessage response)
        {
            if (response.Headers.TryGetValues("set-cookie", out var cookies))
            {
                return(cookies
                       .Select(c => HttpCookie.TryParse(c, out var cookie) ? cookie : null)
                       .Where(c => c != null & c.Name == ".ASPXAUTH")
                       .FirstOrDefault());
            }

            return(null);
        }
        /// <param name="input">input to be parsed</param>
        /// <param name="defaultDomain">default domain for the cookie (used if not specified)</param>
        /// <param name="utcNow">current date and time in UTC</param>
        /// <param name="defaultPath">default path for the cookie (used if not specified)</param>
        /// <returns>a CookieCollection representing the cookie specification in the input</returns>
        public static CookieCollection ParseCookies(string input, string defaultDomain, DateTime utcNow,
                                                    string defaultPath = "/")
        {
            var collection = new CookieCollection();
            var lines      = input.SplitLines();

            foreach (var line in lines)
            {
#if NET48
                var cookieText = UpdateExpiresFromMaxAge(line, utcNow);

                if (HttpCookie.TryParse(cookieText, out var httpCookie))
                {
                    var cookie = new Cookie
                    {
                        Name     = httpCookie.Name,
                        Value    = httpCookie.Value,
                        Domain   = httpCookie.Domain ?? defaultDomain,
                        Expires  = httpCookie.Expires,
                        Path     = httpCookie.Path,
                        HttpOnly = httpCookie.HttpOnly,
                        Secure   = httpCookie.Secure
                    };

                    /*if (string.IsNullOrEmpty(cookie.Domain))
                     * {
                     *  throw new ArgumentException($"Set CookieDomain or specify domain in the cookie specification for '{line}'");
                     * } */
                    collection.Add(cookie);
                }
                else
                {
                    throw new ArgumentException($"Could not parse '{line}' as a cookie");
                }
#else
                var cookieText = RemoveInvalidCookieOptions(line);
                cookieText = UpdateExpiresFromMaxAge(cookieText, utcNow);

                if (SetCookieHeaderValue.TryParse(cookieText, out var httpCookie))
                {
                    var cookie = new Cookie
                    {
                        Name     = httpCookie.Name.Value,
                        Value    = httpCookie.Value.Value,
                        Domain   = httpCookie.Domain.Value ?? defaultDomain,
                        Path     = httpCookie.Path.Value ?? defaultPath,
                        HttpOnly = httpCookie.HttpOnly,
                        Secure   = httpCookie.Secure
                    };

                    if (httpCookie.Expires != null)
                    {
                        cookie.Expires = httpCookie.Expires.Value.UtcDateTime;
                    }

                    /* if (string.IsNullOrEmpty(cookie.Domain))
                     *  throw new ArgumentException(
                     *      $"Set CookieDomain or specify domain in the cookie specification for '{line}'"); */
                    collection.Add(cookie);
                }
                else
                {
                    throw new ArgumentException($"Could not parse '{line}' as a cookie");
                }
#endif
            }

            return(collection);
        }