Exemplo n.º 1
0
        /// <summary>
        /// 将用户登录成功信息写入Cookie
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="Remembered"></param>
        private void ResponseLoginCookie(Administrator entity, bool Remembered = false)
        {
            DateTime expiration = Remembered ? DateTime.Now.AddDays(7) : DateTime.Now.Add(FormsAuthentication.Timeout);
            var      userData   = new { Id = entity.Id, AdminName = entity.Member.MemberName, RealName = entity.Member.RealName };
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, (userData.AdminName + userData.Id.ToString()), DateTime.Now, expiration, Remembered, userData.ToJsonString(), FormsAuthentication.FormsCookiePath);
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))
            {
                HttpOnly = true,
                Secure   = FormsAuthentication.RequireSSL,
                Domain   = FormsAuthentication.CookieDomain,
                Path     = FormsAuthentication.FormsCookiePath,
            };

            if (Remembered)
            {
                cookie.Expires = DateTime.Now.AddDays(7);
            }
            Response.Cookies.Remove(cookie.Name);
            Response.Cookies.Add(cookie);

            #region 临时客户端tcid = 11.1.1.111;Windows 10;Chrome;2016/11/25 10:30:15
            try
            {
                var hostaddress = Request.UserHostAddress;

                //var hostname = Request.LogonUserIdentity.Name;//获取得不正确,暂时弃用

                var        systemname  = UserAgentHelper.GetOperatingSystemName(Request.UserAgent);
                var        browsername = UserAgentHelper.GetBrowserName(Request.UserAgent);
                var        logintime   = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                var        tcid        = AesHelper.Encrypt(string.Format("{0};{1};{2};{3}", hostaddress, systemname, browsername, logintime), userData.Id.ToString());
                HttpCookie tcCookie    = new HttpCookie("tcid", tcid);
                tcCookie.Expires  = cookie.Expires;
                tcCookie.HttpOnly = cookie.HttpOnly;
                tcCookie.Secure   = cookie.Secure;
                tcCookie.Domain   = cookie.Domain;
                tcCookie.Path     = cookie.Path;
                Response.Cookies.Add(tcCookie);
            }
            catch { }
            #endregion
        }