예제 #1
0
        /// <summary>
        /// Deletes the cookie with the given key by setting an expired state. If a matching chunked cookie exists on
        /// the request, delete each chunk.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <param name="options"></param>
        public void DeleteCookie(IOwinContext context, string key, CookieOptions options)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            var webContext = context.Get <HttpContextBase>(typeof(HttpContextBase).FullName);

            if (webContext == null)
            {
                Fallback.DeleteCookie(context, key, options);
                return;
            }

            string escapedKey = Uri.EscapeDataString(key);

            var    requestCookies = webContext.Request.Cookies;
            var    cookie         = requestCookies[escapedKey];
            string requestCookie  = (cookie == null ? null : cookie.Value);

            int chunks = ParseChunksCount(requestCookie);

            AppendResponseCookie(
                context,
                key,
                string.Empty,
                new CookieOptions
            {
                Path     = options.Path,
                Domain   = options.Domain,
                HttpOnly = options.HttpOnly,
                SameSite = options.SameSite,
                Secure   = options.Secure,
                Expires  = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
            });

            for (int i = 1; i <= chunks; i++)
            {
                AppendResponseCookie(
                    context,
                    key + "C" + i.ToString(CultureInfo.InvariantCulture),
                    string.Empty,
                    new CookieOptions
                {
                    Path     = options.Path,
                    Domain   = options.Domain,
                    HttpOnly = options.HttpOnly,
                    SameSite = options.SameSite,
                    Secure   = options.Secure,
                    Expires  = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                });
            }
        }