AdolphYang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

//尝试用户自动登录

/// <summary>
        /// 尝试自动登录 或填充密码
        /// </summary>
        /// <param name="context"></param>
        /// <returns>自动登录后的状态:成功,填充密码,失败继续</returns>
        public static ConstStringHelper.AutoLoginResult TryAutoLoginOrMemoryPwd(HttpContext context,out string username,out string password)
        {
            username = ""; password = "";
            //尝试自动登录
            HttpCookie cookie_chkAutoLogin = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_CHKAUTOLOGIN];
            if (cookie_chkAutoLogin!=null && cookie_chkAutoLogin.Value=="true") 
            {
                HttpCookie cookie_username = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_USERNAME];
                HttpCookie cookie_password = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_PASSWORD];
                if (cookie_username != null && cookie_password!=null)
                {
                    if (CheckLoginStatus(context, cookie_username.Value, CommonHelper.DesDecode(cookie_password.Value)) == ConstStringHelper.LoginResult.OK)
                    {
                        return ConstStringHelper.AutoLoginResult.AutoLogin;
                    }
                }
            }
            //尝试填充密码
            HttpCookie cookie_chkMemoryPwd = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_CHKMEMORYPWD];
            if (cookie_chkMemoryPwd != null && cookie_chkMemoryPwd.Value == "true")
            {
                HttpCookie cookie_username = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_USERNAME];
                HttpCookie cookie_password = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_PASSWORD];
                if (cookie_username != null && cookie_password != null)
                {
                    username = cookie_username.Value;
                    password = CommonHelper.DesDecode(cookie_password.Value);
                    return ConstStringHelper.AutoLoginResult.MemoryPwd;
                }
            }
            return ConstStringHelper.AutoLoginResult.NO;
        }

 

//存Cookie

/// <summary>
        /// 把用户名和密码 存入cookie
        /// </summary>
        /// <param name="context"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static void StoreCookie(HttpContext context, string chkMemoryPwd, string chkAutoLogin, string username, string password)
        {
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_CHKMEMORYPWD) { Value = chkMemoryPwd, Expires = DateTime.Now.AddDays(7) });
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_CHKAUTOLOGIN) { Value = chkAutoLogin, Expires = DateTime.Now.AddDays(7) });
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_USERNAME) { Value = username, Expires = DateTime.Now.AddDays(7) });
            context.Response.SetCookie(new HttpCookie(ConstStringHelper.ADMINCOOKIE_PASSWORD) { Value = CommonHelper.DesEncode(password), Expires = DateTime.Now.AddDays(7) });
        }

 

//存Session

/// <summary>
        /// 把用户id和用户名 存入session 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="username"></param>
        public static void StoreSession(HttpContext context,long id,string username)
        {
            context.Session[ConstStringHelper.ADMINSESSION_ID] = id;
            context.Session[ConstStringHelper.ADMINSESSION_USERNAME] = username;
        }

 

posted on 2015-09-27 11:16  AdolphYang  阅读(222)  评论(0编辑  收藏  举报