Skip to content
This repository
tree: a06cc08f86
Fetching contributors…

Cannot retrieve contributors at this time

file 94 lines (80 sloc) 2.365 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
using System.Collections.Generic;
using System.Linq;

namespace Simple.Web.AspNet
{
    using System;
    using System.Web;
    using Http;

    public class CookieWrapper : ICookie
    {
        private readonly HttpCookie _cookie;
        public string Name
        {
            get { return _cookie.Name; }
            set { _cookie.Name = value; }
        }

        public bool Secure
        {
            get { return _cookie.Secure; }
            set { _cookie.Secure = value; }
        }

        public bool HttpOnly
        {
            get { return _cookie.HttpOnly; }
            set { _cookie.HttpOnly = value; }
        }

        public DateTime Expires
        {
            get { return _cookie.Expires; }
            set { _cookie.Expires = value; }
        }

        public string Value
        {
            get { return _cookie.Value; }
            set { _cookie.Value = value; }
        }

        public IDictionary<string, string> Values
        {
            get
            {
                return
                    _cookie.Values.AllKeys.Select(s => new KeyValuePair<string, string>(s, _cookie.Values[s])).
                        ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }
            set
            {
                foreach (var kvp in value)
                {
                    _cookie.Values[kvp.Key] = kvp.Value;
                }
            }
        }

        public string this[string key]
        {
            get { return _cookie[key]; }
            set { _cookie[key] = value; }
        }

        internal HttpCookie HttpCookie
        {
            get { return _cookie; }
        }

        public CookieWrapper(string name, string value) : this(new HttpCookie(name, value))
        {
        }

        public CookieWrapper(string name) : this(new HttpCookie(name))
        {
        }

        public CookieWrapper(HttpCookie cookie)
        {
            _cookie = cookie;
        }

        public static ICookie Wrap(HttpCookie cookie)
        {
            return new CookieWrapper(cookie);
        }

        public static IEnumerable<ICookie> Wrap(HttpCookieCollection collection)
        {
            return collection.Cast<string>().Select(n => Wrap(collection[n]));
        }
    }
}
Something went wrong with that request. Please try again.