Example #1
0
        /// <summary>
        /// 设置 Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expires">到期日期</param>
        /// <param name="domain">域</param>
        /// <param name="path">虚拟路径</param>
        /// <param name="httpOnly">是否可通过客户端脚本访问</param>
        /// <param name="secure">是否使用安全套接字层 (SSL)(即仅通过 HTTPS)传输 Cookie</param>
        public static void Set(string name, string key, string value, DateTime expires, string domain, string path, bool httpOnly, bool secure)
        {
            string     _key         = "9cf8d21d394a8919d2f9706dfdc6421e";
            string     encryptName  = (_key + name).MD5();
            string     encryptKey   = (_key + key).MD5();
            string     encryptValue = value.AESEncode(_key);
            HttpCookie cookie       = HttpContext.Current.Request.Cookies[encryptName];

            if (cookie.IsNull())
            {
                cookie = new HttpCookie(encryptName);
            }
            cookie.Values[encryptKey] = encryptValue;
            if (expires > DateTime.MinValue)
            {
                cookie.Expires = expires;
            }
            if (!string.IsNullOrEmpty(domain))
            {
                cookie.Domain = domain;
            }
            if (!string.IsNullOrEmpty(path))
            {
                cookie.Path = path;
            }
            cookie.HttpOnly = httpOnly;
            cookie.Secure   = secure;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
Example #2
0
        public bool IsAuthenticated()
        {
            try
            {
                if (WebContext.IsLogged)
                {
                    return(true);
                }

                if (Request.IsNull())
                {
                    return(true);
                }

                if (WebContext.IsLogged)
                {
                    return(true);
                }

                HttpCookie cookieUser = Request.Cookies[CT_UserCookie];
                HttpCookie cookiePwd  = Request.Cookies[CT_PasswordCookie];

                if (cookiePwd.IsNull() || cookieUser.IsNull())
                {
                    return(false);
                }

                AppUser user = CheckCredentials(Cryptography.Decript(cookieUser.Value), Cryptography.Decript(cookiePwd.Value));

                if (user.HasValue())
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        }