コード例 #1
0
        public void DeserializeNoDataTest()
        {
            var data     = new byte[] { };
            var messages = FlashMessage.Deserialize(data);

            Assert.Equal(0, messages.Count);
        }
コード例 #2
0
        public void SerializeEmptyTest()
        {
            List <FlashMessageModel> messages = new List <FlashMessageModel>();
            var data = FlashMessage.Serialize(messages);

            messages = FlashMessage.Deserialize(data);

            Assert.Equal(0, messages.Count);
        }
コード例 #3
0
        public void Queue(IList <FlashMessageModel> messages)
        {
            // Serialize the messages.
            var data = FlashMessage.Serialize(messages);

            // Set the data without doing any further securing or transformations.
            var context = new HttpContextWrapper(HttpContext.Current);

            context.Session[KeyName] = data;
        }
コード例 #4
0
        public void Queue()
        {
            var tempData = new Mock <ITempDataDictionaryFactory>();

            tempData.Setup(x => x.GetTempData(It.IsAny <HttpContext>())).Returns(_tempDataMock.Object);
            var contextAccessor = new Mock <IHttpContextAccessor>();
            var flashMessage    = new Vereyon.Web.FlashMessage(tempData.Object, contextAccessor.Object);

            flashMessage.Info("Test");
        }
コード例 #5
0
        /// <summary>
        /// Renders any queued flash messages to and returns the html code.
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static IHtmlContent RenderFlashMessages(this IHtmlHelper html)
        {
            // Retrieve queued messages.
            var messages = FlashMessage.Retrieve(html.TempData);
            var output   = "";

            foreach (var message in messages)
            {
                output += RenderFlashMessage(message);
            }

            return(new HtmlString(output));
        }
コード例 #6
0
        /// <summary>
        /// Renders any queued flash messages to and returns the html code.
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static IHtmlString RenderFlashMessages(this HtmlHelper html)
        {
            // Retrieve queued messages.
            var messages = FlashMessage.Retrieve(html.ViewContext.HttpContext);
            var output   = "";

            foreach (var message in messages)
            {
                output += RenderFlashMessage(message);
            }

            return(html.Raw(output));
        }
コード例 #7
0
        public List <FlashMessageModel> Peek()
        {
            // Retrieve the data from the session store, guard for cases where it does not exist.
            var context = new HttpContextWrapper(HttpContext.Current);
            var data    = context.Session[KeyName];

            if (data == null)
            {
                return(new List <FlashMessageModel>());
            }

            // Deserialize messages and return them.
            return(FlashMessage.Deserialize((byte[])data));
        }
コード例 #8
0
        public List <FlashMessageModel> Peek()
        {
            // Attempt to retrieve cookie. If the cookie is non existent, return an empty message list.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = context.Request.Cookies[CookieName];

            if (cookie == null)
            {
                return(new List <FlashMessageModel>());
            }

            // Decode and deserialize the data.
            var serializedMessages = MachineKey.Decode(cookie.Value, MachineKeyProtection.All);

            return(FlashMessage.Deserialize(serializedMessages));
        }
コード例 #9
0
        public List <FlashMessageModel> GetQueued()
        {
            // Attempt to retrieve cookie.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = context.Response.Cookies[CookieName];

            // If the cookie is non existent, return an empty message list.
            // If the cookie value is null, also return an empty list, as MachineKey.Decode does not like empty strings.
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                return(new List <FlashMessageModel>());
            }

            // Decode and deserialize the data.
            var serializedMessages = MachineKey.Decode(cookie.Value, MachineKeyProtection.All);

            return(FlashMessage.Deserialize(serializedMessages));
        }
コード例 #10
0
        public void Queue(IList <FlashMessageModel> messages)
        {
            // Serialize and digitally sign the data.
            // Encoding/signing is done using the configured encryption and key in the machineKey element.
            // TODO: Replace with MachineKey.Protect for .Net Framework >= 4.5
            var serializedMessages = FlashMessage.Serialize(messages);
            var data = MachineKey.Encode(serializedMessages, MachineKeyProtection.All);

            // Serialize messages and enforce cookie size limit.
            if (data.Length > CookieSizeLimit && CookieSizeLimit > 0)
            {
                throw new InvalidOperationException("The flash messages cookie size limit exceeded the limit value. Queue less messages.");
            }

            // Set the cookie.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = new HttpCookie(CookieName, data);

            context.Response.SetCookie(cookie);
        }
コード例 #11
0
        public void SerializeSingleTest()
        {
            FlashMessageModel message = new FlashMessageModel
            {
                IsHtml  = true,
                Message = "<p>test content</p> üñï€ode",
                Title   = "Title",
                Type    = FlashMessageType.Custom
            };

            // Serialize and deserialize.
            FlashMessage.Queue(message);
            var messages     = FlashMessage.GetQueued();
            var deserialized = messages[0];

            // Compare the results.
            Assert.Equal(message.Type, deserialized.Type);
            Assert.Equal(message.Title, deserialized.Title);
            Assert.Equal(message.Message, deserialized.Message);
            Assert.Equal(message.IsHtml, message.IsHtml);
        }
コード例 #12
0
        public List <FlashMessageModel> Retrieve()
        {
            // Attempt to retrieve cookie. If the cookie is non existent, return an empty message list.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = context.Request.Cookies[CookieName];

            if (cookie == null)
            {
                return(new List <FlashMessageModel>());
            }

            var data = cookie.Value;

            // Clear the cookie by setting it to expired.
            cookie.Value   = null;
            cookie.Expires = DateTime.Now.AddDays(-1);
            context.Response.SetCookie(cookie);

            // Decode and deserialize the data.
            var serializedMessages = MachineKey.Decode(data, MachineKeyProtection.All);

            return(FlashMessage.Deserialize(serializedMessages));
        }
コード例 #13
0
        public void SerializeSingleTest()
        {
            List <FlashMessageModel> messages = new List <FlashMessageModel>();
            FlashMessageModel        message  = new FlashMessageModel
            {
                IsHtml  = true,
                Message = "<p>test content</p> üñï€ode",
                Title   = "Title",
                Type    = FlashMessageType.Custom
            };

            // Serialize and deserialize.
            messages.Add(message);
            var data = FlashMessage.Serialize(messages);

            messages = FlashMessage.Deserialize(data);
            var deserialized = messages[0];

            // Compare the results.
            Assert.Equal(message.Type, deserialized.Type);
            Assert.Equal(message.Title, deserialized.Title);
            Assert.Equal(message.Message, deserialized.Message);
            Assert.Equal(message.IsHtml, message.IsHtml);
        }