Exemple #1
0
        public static string GenerateExpiredCookieValue()
        {
            var    expiration         = DateTime.UtcNow.AddDays(-1);
            string compositeToken     = Guid.Empty.ToString();
            string compositeEwName    = "";
            string compositeEwRole    = "";
            string compositeExpiresOn = expiration.Ticks.ToString();
            string composite          = string.Format("{0}|{1}|{2}|{3}", compositeToken, compositeEwName, compositeEwRole, compositeExpiresOn);

            return(TextEncrytion.EncryptString(composite, secretKey));
        }
Exemple #2
0
        private static void RefreshCookie(CookieInfo info)
        {
            var    compositeToken     = Guid.NewGuid().ToString();
            var    compositeEwName    = info.EarthwatcherName;
            var    compositeEwRole    = string.Join(",", info.Roles);
            var    expiresOn          = info.KeepLogged ? DateTime.UtcNow.AddDays(15) : DateTime.UtcNow.AddMinutes(30);
            var    compositeExpiresOn = expiresOn.ToString();
            string composite          = string.Format("{0}|{1}|{2}|{3}|{4}", compositeToken, compositeEwName, compositeEwRole, compositeExpiresOn, info.KeepLogged);

            string strToken = TextEncrytion.EncryptString(composite, Session.secretKey);

            HttpCookie cookie = new HttpCookie(Session.TOKENKEY);

            cookie.Value    = strToken;
            cookie.HttpOnly = false;
            cookie.Expires  = expiresOn;
            System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
        }
Exemple #3
0
        public static string GenerateCookie(Earthwatcher ew, bool keeplogued = true)
        {
            string compositeToken     = Guid.NewGuid().ToString();
            string compositeEwName    = ew.Name;
            string compositeEwRole    = string.Join(",", ew.GetRoles());
            var    expiresOn          = keeplogued ? DateTime.UtcNow.AddDays(15) : DateTime.UtcNow.AddMinutes(30);
            var    compositeExpiresOn = expiresOn.ToString();
            string composite          = string.Format("{0}|{1}|{2}|{3}|{4}", compositeToken, compositeEwName, compositeEwRole, compositeExpiresOn, keeplogued);

            string strToken = TextEncrytion.EncryptString(composite, Session.secretKey);

            HttpCookie cookie = new HttpCookie(Session.TOKENKEY);

            cookie.Value    = strToken;
            cookie.Expires  = expiresOn;
            cookie.HttpOnly = false;
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

            return(strToken);
        }
Exemple #4
0
        public static CookieInfo GetCookieInfo()
        {
            CookieInfo info = new CookieInfo();

            try
            {
                HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[Session.TOKENKEY];
                if (cookie == null)
                {
                    return(info);
                }

                string   cookieCompositeValue = cookie.Value;
                string   composite            = TextEncrytion.DecryptString(cookieCompositeValue, Session.secretKey);
                string[] compositeValues      = composite.Split('|');

                info.LoginToken       = new Guid(compositeValues[0]);
                info.EarthwatcherName = compositeValues[1];
                info.Roles            = compositeValues[2].Split(',');
                info.ExpiresOn        = DateTime.Parse(compositeValues[3]);
                info.KeepLogged       = Convert.ToBoolean(compositeValues[4]);

                if ((DateTime.UtcNow - info.ExpiresOn).Minutes > 1)
                {
                    //Borrar cookie
                    DeleteCookie();
                    return(new CookieInfo());
                }
                else
                {
                    RefreshCookie(info);
                }
            }
            catch
            {
            }

            return(info);
        }