Example #1
0
 /// <summary>
 /// 清除cookie
 /// </summary>
 /// <param name="response"></param>
 public static void ClearCookie(HttpResponseBase response)
 {
     response.AppendCookie(new HttpCookie(WebConfigs.CookieName)
     {
         Expires = DateTime.Now.AddDays(-1)
     });
     //设置指定域名cookie设置过期
     response.AppendCookie(new HttpCookie(WebConfigs.CookieName)
     {
         Domain  = WebConfigs.CookieDomain,
         Expires = DateTime.Now.AddDays(-1)
     });
 }
Example #2
0
        /// <summary>
        /// 添加cookie
        /// </summary>
        /// <param name="response">Http响应</param>
        /// <param name="cookieName">cookieName</param>
        /// <param name="value">cookieValue</param>
        /// <param name="cookieDomain">cookie关联的域</param>
        /// <param name="expires">cookie有效期</param>
        /// <param name="isSetExpires">是否设置有效期</param>
        /// <param name="Path">要与当前 Cookie 一起传输的虚拟路径</param>
        /// <param name="HttpOnly"> Cookie 是否可通过客户端脚本访问 true代表客户端只能读,不能写。只有服务端可写,防止被篡改</param>
        /// <param name="Values">Cookie 对象所包含的键值对的集合</param>
        /// <param name="Secure">是否使用安全套接字层 (SSL)(即仅通过 HTTPS)传输 Cookie</param>
        /// <param name="Shareable">确定 cookie 是否允许参与输出缓存</param>
        public void AddCookie(HttpResponseBase response, string cookieName, string value, string cookieDomain, DateTime expires
                              , bool isSetExpires = true, string Path = "/", bool HttpOnly = true, NameValueCollection Values = null, bool Secure = false, bool Shareable = false)
        {
            var cookie = response.Cookies[cookieName] ?? new HttpCookie(cookieName);

            cookie.Value     = value;
            cookie.Domain    = cookieDomain;
            cookie.Path      = Path;
            cookie.Secure    = Secure;
            cookie.Shareable = Shareable;
            //true代表客户端只能读,不能写。只有服务端可写,防止被篡改
            cookie.HttpOnly = HttpOnly;

            if (Values != null)
            {
                cookie.Values.Add(Values);
            }

            if (isSetExpires)
            {
                cookie.Expires = expires;
            }

            response.AppendCookie(cookie);
        }
Example #3
0
        public static void SaveTwoFactorAuthenticationToken(CMSDataContext db, HttpResponseBase response)
        {
            const string name = "_mfa";

            var expirationDays = db.Setting("TwoFactorAuthExpirationDays", "30").ToInt();
            var expires        = DateTime.Now.AddDays(expirationDays);
            var key            = string.Join("", "123".Select(c => Guid.NewGuid().ToString("N")));
            var token          = new MFAToken {
                Expires = expires,
                Key     = key,
                UserId  = Util.UserId
            };

            db.MFATokens.InsertOnSubmit(token);
            db.SubmitChanges();

            var cookie = new HttpCookie(name, token.Key)
            {
                Expires = expires, HttpOnly = true, Secure = !Util.IsDebug()
            };

            if (!cookie.Secure) // https://stackoverflow.com/questions/26627886/not-able-to-set-cookie-from-action
            {
                cookie.Domain = null;
            }
            response.AppendCookie(cookie);
        }
Example #4
0
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="channelCode"></param>
        private void AddCache(string channelCode, HttpResponseBase Response)
        {
            string cInvitedcode = channelCode;

            if (string.IsNullOrEmpty(cInvitedcode))
            {//此前放出了一批错误连接channnel
                cInvitedcode = Utils.CheckSQLHtml(cInvitedcode);
            }
            if (!string.IsNullOrEmpty(cInvitedcode))
            {
                var keyValue = new Dictionary <string, string>();
                keyValue.Add("Invitedcode", cInvitedcode);
                Utils.SetInvCookie("channel", keyValue);
            }

            var invitedcode = Utils.CheckSQLHtml("invitedcode");

            if (!string.IsNullOrWhiteSpace(invitedcode))
            {
                string sql = "select registerid,invitedcode from hx_member_table where invitedcode='" + invitedcode + "' ";

                DataTable dt = DbHelperSQL.GET_DataTable_List(sql);
                if (dt.Rows.Count > 0)
                {
                    HttpCookie c*k = new HttpCookie("Invitation");
                    c*k.Values.Add("InvCode", DESEncrypt.Encrypt(invitedcode, _webp));
                    c*k.Values.Add("CodeUid", DESEncrypt.Encrypt(dt.Rows[0]["registerid"].ToString(), _webp));
                    c*k.Expires = DateTime.Now.AddDays(30);
                    Response.AppendCookie(c*k);
                }
            }
        }
        public static void AccessTokenSave(HttpResponseBase response, String token)
        {
            var cookie = new HttpCookie(CookieName, token)
            {
                Expires = DateTime.Now.AddDays(1)
            };

            response.AppendCookie(cookie);
        }
        public void append_a_cookie_to_response_when_successfull_login()
        {
            var cookie = Isolate.Fake.Instance <HttpCookie>();

            Isolate.WhenCalled(() => LoginServices.IsLoggedIn(ValidUser)).WillReturn(true);
            Isolate.WhenCalled(() => _controller.Response).WillReturn(_response);

            SetEnvironmentOnController();
            _controller.Login(_user);

            Isolate.Verify.WasCalledWithAnyArguments(() => _response.AppendCookie(cookie));
        }
Example #7
0
        /// <summary>
        /// Authenticates a user via the MembershipProvider and creates the associated forms authentication ticket.
        /// </summary>
        /// <param name="logon">Logon</param>
        /// <param name="response">HttpResponseBase</param>
        /// <returns>bool</returns>
        public static bool ValidateUser(Logon logon, HttpResponseBase response)
        {
            bool result = false;

            if (Membership.ValidateUser(logon.Username, logon.Password))
            {
                // Create the authentication ticket with custom user data.
                var    serializer = new JavaScriptSerializer();
                string userData   = serializer.Serialize(UserManager.User);

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                                                 logon.Username,
                                                                                 DateTime.Now,
                                                                                 DateTime.Now.AddDays(30),
                                                                                 true,
                                                                                 userData,
                                                                                 FormsAuthentication.FormsCookiePath);

                // Encrypt the ticket.
                string encTicket = FormsAuthentication.Encrypt(ticket);

                //encTicket = ZipLib.Zip(encTicket);
                // Create the cookie.

                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                cookie.Expires = DateTime.Now.AddDays(1);
                cookie.Value   = encTicket;
                response.AppendCookie(cookie);

                //response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

                result = true;
            }

            return(result);
        }
 public override void AppendCookie(HttpCookie cookie)
 {
     _httpResponseBase.AppendCookie(cookie);
 }
Example #9
0
 public void AppendCookie(HttpCookie cookie)
 {
     _response.AppendCookie(cookie);
 }
 public override void AppendCookie(HttpCookie cookie)
 {
     proxiedResponse.AppendCookie(cookie);
 }