Exemplo n.º 1
0
        protected override void StoreInternal()
        {
            Logger?.LogCategory($"PlainCookieStateProvider<{typeof(T).Name}>('{Name}').Store()");
            Logger?.Log("Creating cookies ...");

            var cookie     = new HttpCookie(Name);
            var statCookie = new HttpCookie(Name + ".stat");

            if (!string.IsNullOrEmpty(DOMAIN))
            {
                cookie.Domain     = DOMAIN;
                statCookie.Domain = DOMAIN;
            }
            if (!string.IsNullOrEmpty(PATH))
            {
                cookie.Path     = PATH;
                statCookie.Path = PATH;
            }

            cookie.HttpOnly     = true;
            statCookie.HttpOnly = true;

            if (Stat.Removed)
            {
                Logger?.Log("Item is removed. Removing cookies ...");

                cookie.Expires     = DateTime.Now.AddDays(-1d);
                statCookie.Expires = DateTime.Now.AddDays(-1d);
            }
            else
            {
                cookie.Value = GetString(Value);

                var base64 = new Cryptography.Base64Encoder();
                var bytes  = Serializer.Serialize(Stat);
                var str    = Encoding.UTF8.GetString(bytes);
                var chars  = base64.Encode(str, Encoding.UTF8);

                statCookie.Value = new string(chars);
            }

            try
            {
                HttpContext.Response.Cookies.Add(cookie);
                HttpContext.Response.Cookies.Add(statCookie);

                Stored();
            }
            catch (System.Web.HttpException e)
            {
                if (!e.Message.Contains("cannot modify cookies"))
                {
                    ExceptionLogger?.LogException(e);
                }
            }
            catch (Exception e)
            {
                ExceptionLogger?.LogException(e);
            }
        }
Exemplo n.º 2
0
        private string GetString(T value)
        {
            var base64 = new Cryptography.Base64Encoder();

            var bytes = Serializer.Serialize(value);
            var str   = Encoding.UTF8.GetString(bytes);

            str = TransformBeforeWrite(str);
            var chars = base64.Encode(str, Encoding.UTF8);

            str = new string(chars);

            return(str);
        }