Example #1
0
        private static void PublishAuthCookie(HttpContext context, IdentityInfo identityInfo)
        {
            #region 如果支持持久性身份验证Cookie

            string userData = null;
            using (MemoryStream buffer = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(buffer, identityInfo);
                buffer.Position = 0;
                userData        = System.Convert.ToBase64String(buffer.ToArray());
            }


            // 使用本地日期和时间
            FormsAuthenticationTicket tk = new FormsAuthenticationTicket(1, identityInfo.UserName,
                                                                         DateTime.Now, DateTime.Now.AddMinutes((double)Settings.GetAuthCookieTimeOut()),
                                                                         true, userData, Settings.GetAuthCookiePath());
            string encryptedTicket           = FormsAuthentication.Encrypt(tk); //加密身份验票
            System.Web.HttpCookie authCookie = new HttpCookie(Settings.GetAuthCookieName(), encryptedTicket);
            authCookie.Path    = Settings.GetAuthCookiePath();
            authCookie.Domain  = Settings.GetAuthCookieDomain();
            authCookie.Expires = tk.Expiration;
            authCookie.Secure  = Settings.GetAuthCookieRequireSSL();
            context.Response.Cookies.Add(authCookie);

            #endregion
        }
Example #2
0
        /// <summary>
        /// 建立会话
        /// </summary>
        /// <param name="identityInfo"></param>
        /// <param name="sessionContex"></param>
        private static void EstablishSession(IdentityInfo identityInfo, HttpContext sessionContex)
        {
            sessionContex.Session["UserId"] = identityInfo.UserKey;     // 兼容CQS

            sessionContex.Session["UID"]      = identityInfo.UserKey;
            sessionContex.Session["UserName"] = identityInfo.UserName;
            sessionContex.Session["Cookie"]   = true;

            PublishAuthCookie(sessionContex, identityInfo);
        }
Example #3
0
        //
        private void Session_Start(object sender, EventArgs e)
        {
            try
            {
                IdentityInfo identityInfo = GetAuthCookieIdentity(HttpContext.Current);

                if (identityInfo != null)
                {
                    EstablishSession(identityInfo, HttpContext.Current);

                    // BETTER:可以在AuthCookie存储当前会话的内容,并在恢复会话的时候同时恢复内容。
                }
            }
            catch (Exception ex)
            {
                log.Error(String.Format("Session_Start Error(Auto Cookie):{0}", ex.ToString()));
            }
        }