Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web;
- using Freelancers.Infrastructure.Helpers;
- using Freelancers.Infrastructure.Interfaces;
- namespace Freelancers.Infrastructure.Implementations
- {
- public class CookieStorage : ICookieStorage
- {
- public void Store(string key, string value, DateTime expires)
- {
- Ensure.Argument.NotNullOrEmpty(key, "key");
- Ensure.Argument.NotNullOrEmpty(value, "value");
- Ensure.Argument.InFuture(expires, "expires");
- var cookie = HttpContextFactory.Current.Request.Cookies.Get(key);
- if (cookie != null)
- {
- cookie.Value = value;
- cookie.Expires = expires;
- HttpContextFactory.Current.Response.SetCookie(cookie);
- }
- else
- {
- cookie = new HttpCookie(key)
- {
- Value = value,
- Expires = expires
- };
- HttpContextFactory.Current.Response.Cookies.Add(cookie);
- }
- }
- public string Retrieve(string key)
- {
- Ensure.Argument.NotNullOrEmpty(key, "key");
- var cookie = HttpContextFactory.Current.Request.Cookies[key];
- return cookie == null ? null : cookie.Value;
- }
- public void Remove(string key)
- {
- Ensure.Argument.NotNullOrEmpty(key, "key");
- var cookie = HttpContextFactory.Current.Response.Cookies[key];
- if (cookie != null)
- cookie.Expires = DateTime.Now.AddDays(-1);
- HttpContextFactory.Current.Request.Cookies.Remove(key);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment