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, "");
            }
  • 相关阅读:
    ASP.NET MVC中 CKeditor 通过两种方法向后台传值以及编码、乱码问题
    如何解析<textarea>标签中的html代码
    ASP.NET MVC中,后台向前台传递多个对象(表)的方法
    ASP.NET MVC 环境下CKeditor 的配置以及用jQuery进行数据存取操作
    jquery下 动态显示jqGrid 以及jqGrid的属性设置容易出现的问题
    ASP.NET MVC 中 CKeditor.js的正确引用
    关于有道词典的一个小错误
    ASP.NET MVC 标签绑定传值及后台获取并进行修改操作
    每天学点GDB 6
    每天学点GDB 9
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/4824545.html
Copyright © 2011-2022 走看看