zoukankan      html  css  js  c++  java
  • 登录退出代码

    1.登录代码

            /// <summary>
            /// 登陆
            /// </summary>
            /// <param name="userNo"></param>
            /// <param name="password"></param>
            /// <param name="persistCookie"></param>
            /// <returns></returns>
            public static KeyValuePair<bool, string> Login(string userNo, string password, bool persistCookie = false)
            {
                if (HttpContext.Current == null) return new KeyValuePair<bool, string>(false, "请求异常!");
                password = Com.Encrypt(password, key);//密码加密了两次,这里是第一次
    
                //这边需要验证账户密码是否正确,正确之后,才写入cookie
                
                User su = GetUser(userNo);
                HttpContext.Current.Session["CurrentUser"] = su;
                //如果勾选了记住我,则写入cookie
                if (persistCookie)
                {
                    string eUser = Com.Encrypt("User", key);
                    string eUserNo = Com.Encrypt("UserNo", key);
                    string ePassword = Com.Encrypt("Password", key);
                    string eNo = Com.Encrypt(userNo, key);
                    string ePw = Com.Encrypt(password, key);//密码再加密一次
                    HttpCookie Cookie = HttpContext.Current.Request.Cookies[eUser];
                    if (Cookie == null || !Cookie.Values[eUserNo].Equals(eNo))
                    {
                        Cookie = new HttpCookie(eUser);
                        Cookie.Values.Add(eUserNo, eNo);//将账号写入cookie
                        Cookie.Values.Add(ePassword, ePw);//将密码写入cookie。。。这两个cookie的键  也是经过加密的
                        Cookie.Expires = DateTime.Now.AddDays(365);
                        HttpContext.Current.Response.Cookies.Add(Cookie);
                    }
                }
                return new KeyValuePair<bool, string>(true, "");
            }

    2.退出代码

    无非就是获得之前写入的cookie,然后设置它的有效时间

            /// <summary>
            /// 登出
            /// </summary>
            /// <returns></returns>
            public static KeyValuePair<bool, string> Logout()
            {
                if (HttpContext.Current == null) return new KeyValuePair<bool, string>(false, "请求异常!");
                HttpContext.Current.Session["CurrentUser"] = null;
                HttpContext.Current.Session["Authenticated"] = null;
                string eUser = Com.Encrypt("User", key);
                string eUserNo = Com.Encrypt("UserNo", key);
                string ePassword = Com.Encrypt("Password", key);
                HttpCookie Cookie = HttpContext.Current.Request.Cookies[eUser];
                if (Cookie != null)
                {
                    Cookie = new HttpCookie(eUser);
                    Cookie.Expires = DateTime.Now.AddDays(-1);
                    HttpContext.Current.Response.Cookies.Add(Cookie);
                }
                return new KeyValuePair<bool, string>(true, "");
            }
  • 相关阅读:
    JMeter学习-016-思路篇之-山重水复柳暗花明
    JMeter学习-015-JMeter 断言之-Bean Shell Assertion
    JMeter学习-014-JMeter 配置元件实例之
    Fiddler-008-简单模拟性能测试
    Fiddler-007-修改HTTP请求响应数据
    Fiddler-006-修改HTTP请求参数
    JMeter学习-013-JMeter 逻辑控制器之-如果(If)控制器
    JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
    JMeter学习-011-JMeter 后置处理器实例之
    JMeter学习-010-JMeter 配置元件实例之
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/4824545.html
Copyright © 2011-2022 走看看