public void SetCookie(string key, string value, HttpCookieOptions options)
        {
            options = options ?? new HttpCookieOptions();
            var coreOptions = new CookieOptions();

            if (options.Domain != null)
            {
                coreOptions.Domain = options.Domain;
            }
            if (options.Expires != null)
            {
                coreOptions.Expires = options.Expires;
            }
            if (options.HttpOnly != null)
            {
                coreOptions.HttpOnly = options.HttpOnly.Value;
            }
            if (options.Path != null)
            {
                coreOptions.Path = options.Path;
            }
            if (options.Secure != null)
            {
                coreOptions.Secure = options.Secure.Value;
            }
            OwinResponse.Cookies.Append(key, value, coreOptions);
        }
Beispiel #2
0
        public void AppendCookie(string key, string value, HttpCookieOptions opt)
        {
            HttpCookie cookie = new HttpCookie(key, value);

            CopyCookieOptions(cookie, opt);
            Context.Response.AppendCookie(cookie);
        }
Beispiel #3
0
        private static void CopyCookieOptions(HttpCookie cookie, HttpCookieOptions opt)
        {
            if (opt != null)
            {
                if (opt.Expires != null)
                {
                    cookie.Expires = DateTime.Now.Add(opt.Expires.Value.Offset);
                }

                if (opt.Path != null)
                {
                    cookie.Path = opt.Path;
                }

                if (opt.MaxAge != null)
                {
                    cookie.Expires = DateTime.Now.Add(opt.MaxAge.Value);
                }

                if (opt.Domain != null)
                {
                    cookie.Domain = opt.Domain;
                }

                cookie.HttpOnly = opt.HttpOnly;
            }
        }
Beispiel #4
0
        public void DeleteCookie(string key, HttpCookieOptions opt)
        {
            HttpCookie cookie = new HttpCookie(key);

            cookie.Expires = DateTime.Now.AddSeconds(-1 * 10E9);
            CopyCookieOptions(cookie, opt);
        }
Beispiel #5
0
            public static int Login(string username, string password, double minutes)
            {
                var sha1Pwd = Generator.CreateUserPwd(password);
                var result  = ServiceCall.Instance.UserService.TryLogin(username, sha1Pwd);

                if (result.Tag == 1)
                {
                    Exit();
                    var ctx  = HttpHosting.Context;
                    var user = ServiceCall.Instance.UserService.GetUser(result.Uid);
                    ctx.Session.SetObjectAsJson(AdminSk, user);
                    var opt = new HttpCookieOptions();
                    opt.Expires = DateTime.Now.AddMinutes(minutes);
                    // cookie.Domain=AppContext.Config.Domain.HostName;
                    opt.Path = "/" + Settings.SYS_ADMIN_TAG;
                    //保存到Cookie中的密钥
                    var token           = (username + Generator.Salt + sha1Pwd).Md5();
                    var encodeBytes     = Encoding.UTF8.GetBytes(username + "&" + token);
                    var encodedTokenStr = Convert.ToBase64String(encodeBytes);

                    ctx.Response.AppendCookie($"cms_sid_{GeneratorRandomStr()}", encodedTokenStr, opt);
                }

                return(result.Tag);
            }
Beispiel #6
0
        public void SetCookie(string key, string value, HttpCookieOptions options)
        {
            var cookie = new HttpCookie(key, value);

            if (options.Domain != null)
            {
                cookie.Domain = options.Domain;
            }
            if (options.Expires != null)
            {
                cookie.Expires = options.Expires.Value;
            }
            if (options.HttpOnly != null)
            {
                cookie.HttpOnly = options.HttpOnly.Value;
            }
            if (options.Path != null)
            {
                cookie.Path = options.Path;
            }
            if (options.Secure != null)
            {
                cookie.Secure = options.Secure.Value;
            }
            OriginalResponse.Cookies.Add(cookie);
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="value"></param>
        public static void SetCurrentManageSite(ICompatibleHttpContext context, SiteDto value)
        {
            var opt = new HttpCookieOptions();

            opt.Expires = DateTime.Now.AddDays(2);
            opt.Path    = "/" + Settings.SYS_ADMIN_TAG;
            context.Response.AppendCookie(CookieNameKey, value.SiteId.ToString(), opt);
            context.Session.Remove(CurrentSiteSessionStr);
        }
Beispiel #8
0
        /// <summary>
        /// Remove cookie value
        /// </summary>
        /// <param name="context">Http context</param>
        /// <param name="key">Cookie key</param>
        /// <returns></returns>
        public static void RemoveCookie(this IHttpContext context, string key)
        {
            var options = new HttpCookieOptions()
            {
                Expires = new DateTime(1970, 1, 1)
            };

            context.PutCookie(key, "", options);
        }
        /// <summary>
        /// 设置会话Id
        /// </summary>
        /// <param name="sessionId">会话Id</param>
        /// <param name="expires">过期时间</param>
        public void SetSessionId(Guid sessionId, DateTime?expires)
        {
            var options = new HttpCookieOptions()
            {
                Expires = expires, HttpOnly = true
            };
            var context = HttpManager.CurrentContext;

            context.PutCookie(SessionCookieKey, sessionId.ToString(), options);
        }
        /// <summary>
        /// 设置CSRF校验
        /// </summary>
        public void SetCsrfToken(string token)
        {
            var options = new HttpCookieOptions()
            {
                Expires = null, HttpOnly = true
            };
            var context = HttpManager.CurrentContext;

            context.PutCookie(CsrfTokenCookieKey, token, options);
        }
Beispiel #11
0
        /// <summary>
        /// Put cookie value to http context
        /// </summary>
        /// <param name="context">Http context</param>
        /// <param name="key">Cookie key</param>
        /// <param name="value">Cookie value</param>
        /// <param name="options">Cookie options</param>
        /// <returns></returns>
        public static void PutCookie(
            this IHttpContext context, string key, string value, HttpCookieOptions options = null)
        {
            // Record the value to http context
            var dataKey = SetCookieDataKeyPrefix + key;

            context.PutData(dataKey, value);
            // Set cookie value to http response
            var cookie = HttpUtils.UrlEncode(value);

            context.Response.SetCookie(key, cookie, options ?? new HttpCookieOptions());
        }
Beispiel #12
0
 private CookieOptions ParseOptions(HttpCookieOptions opt)
 {
     return(new CookieOptions
     {
         Domain = opt.Domain,
         Expires = opt.Expires,
         HttpOnly = opt.HttpOnly,
         IsEssential = opt.IsEssential,
         MaxAge = opt.MaxAge,
         Path = opt.Path,
         Secure = opt.Secure,
     });
 }
Beispiel #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool SetUserDevice(int device)
        {
            if (Enum.IsDefined(typeof(DeviceType), device))
            {
                var same = device == (int)this._userDevice;
                // _userDevice = (DeviceType) device; //保存
                var opt = new HttpCookieOptions
                {
                    Expires  = DateTime.Now.AddHours((same?-24:24) * 365),
                    Path     = SiteAppPath,
                    HttpOnly = true
                };
                _context.Response.AppendCookie(UserDeviceCookieName, device.ToString(), opt);
                //SetSessionUserDeviceSet(device);
                return(true);
            }

            return(false);
        }
Beispiel #14
0
            public static void Exit()
            {
                var ctx = HttpHosting.Context;

                //UserBll user = Current;
                //移除会话
                ctx.Session.Remove(AdminSk);
                //移除Cookie
                foreach (var key in ctx.Request.CookiesKeys())
                {
                    if (Regex.IsMatch(key.ToString(), AdministratorTokenPattern))
                    {
                        var opt = new HttpCookieOptions();
                        opt.Expires = DateTime.Now.AddYears(-1);
                        opt.Path    = "/" + Settings.SYS_ADMIN_TAG;
                        ctx.Response.DeleteCookie(key, opt);
                    }
                }
            }
Beispiel #15
0
        /// <summary>
        /// 设置用户的语言
        /// </summary>
        /// <param name="lang"></param>
        /// <returns></returns>
        public bool SetUserLanguage(int lang)
        {
            if (Enum.IsDefined(typeof(Languages), lang))
            {
                var sameLang = lang == (int)this.CurrentSite.Language;
                _userLanguage = (Languages)lang;  //保存
                var opt = new HttpCookieOptions
                {
                    Expires  = DateTime.Now.AddHours((sameLang ? -24 : 24) * 365),
                    Path     = SiteAppPath,
                    HttpOnly = true
                };
                _context.Response.AppendCookie(UserLanguageCookieName, lang.ToString(), opt);

                //SetSessionLangSet(lang);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// 添加或更新当前的会话
        /// 必要时发送Cookie到浏览器
        /// </summary>
        public virtual void SaveSession()
        {
            var context = HttpManager.CurrentContext;
            var session = context.GetData <Session>(SessionKey, null);

            if (session == null)
            {
                throw new NullReferenceException("session is null");
            }
            // 添加或更新到数据库中
            var cookieSessionId = context.GetCookie(SessionKey);

            UnitOfWork.WriteData <Session>(r => {
                // 保存会话
                r.Save(ref session);
                // 检测到会话Id有变化时删除原会话
                if (cookieSessionId != session.Id)
                {
                    r.BatchDelete(s => s.Id == cookieSessionId);
                }
            });
            // 发送会话Cookies到客户端
            // 已存在且过期时间没有更新时不会重复发送
            if (cookieSessionId != session.Id || session.ExpiresUpdated)
            {
                session.ExpiresUpdated = false;
                DateTime?expires = null;
                if (session.RememberLogin)
                {
                    expires = session.Expires.AddYears(1);
                }
                var options = new HttpCookieOptions()
                {
                    Expires = expires, HttpOnly = true
                };
                context.PutCookie(SessionKey, session.Id, options);
            }
        }
Beispiel #17
0
 public void AppendCookie(string key, string value, HttpCookieOptions opt)
 {
     this._accessor.HttpContext.Response.Cookies.Append(key, value, this.ParseOptions(opt));
 }
 public virtual void SetCookie(string key, string value, HttpCookieOptions options)
 {
     OriginalResponse.SetCookie(key, value, options);
 }
Beispiel #19
0
 public virtual void SetCookie(string key, string value, HttpCookieOptions options)
 {
     cookies[key] = value;
 }
Beispiel #20
0
 public void DeleteCookie(string key, HttpCookieOptions opt)
 {
     this._accessor.HttpContext.Response.Cookies.Delete(key, this.ParseOptions(opt));
 }