コード例 #1
0
        public ActionResult CheckLogin()
        {//验证码初次不显示功能,实现为加缓存,缓存每10分钟清空一次。如果加到数据库会增加压力
            string userName = Request["LoginCode"];

            if (SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache.ContainsKey(userName))
            {
                short FailCount = SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[userName];
                if (FailCount > 3)
                {
                    #region 需要验证码,成功登陆需重置验证码
                    string validateCode = Session["validateCode"] == null ? string.Empty : Session["validateCode"].ToString();
                    if (string.IsNullOrEmpty(validateCode))
                    {
                        return(Content("no:验证码错误!:" + FailCount.ToString()));
                    }
                    Session["validateCode"] = null;
                    string requestCode = Request["vCode"];
                    if (!requestCode.Equals(validateCode, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(Content("no:验证码错误!:" + FailCount.ToString()));
                    }
                    //string userName = Request["LoginCode"];
                    string userPwd  = Request["LoginPwd"];
                    var    userInfo = userInfoService.LoadEntities(u => u.UName == userName && u.UPwd == userPwd).FirstOrDefault();//对用户名密码进行过滤.
                    if (userInfo == null)
                    {
                        return(Content("no:用户名或密码错误!:" + FailCount.ToString()));
                    }
                    else
                    {
                        Session["userInfo"] = userInfo;//低配版本直接存Session
                        //string sessionId = Guid.NewGuid().ToString();//自己创建的SessionId,作为Memcache的key.
                        //Common.MemcacheHelper.Set(sessionId, Common.SerializerHelper.SerializerToString(userInfo));//将用户的信息存储到Memcache中。
                        //Response.Cookies["sessionId"].Value = sessionId;//然后将自创的sessionId以Cookie的形式返回到浏览器,存储到浏览器端的内存中。
                        //判断一下用户是否选择了记住我.
                        if (!string.IsNullOrEmpty(Request["checkMe"]))
                        {
                            HttpCookie cookie1 = new HttpCookie("cp1", userName);                                                        //用户名
                            HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.Md5String(Common.WebCommon.Md5String(userPwd))); //密码2次MD5加密,更安全
                            cookie1.Expires = DateTime.Now.AddDays(3);                                                                   //记住多少天
                            cookie2.Expires = DateTime.Now.AddDays(3);
                            Response.Cookies.Add(cookie1);
                            Response.Cookies.Add(cookie2);
                        }
                        SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[userName] = 0;
                        return(Content("ok:"));
                    }
                    #endregion
                }
                else
                {
                    SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[userName]++;
                    #region  需要验证码,成功登陆需重置验证码
                    string userPwd  = Request["LoginPwd"];
                    var    userInfo = userInfoService.LoadEntities(u => u.UName == userName && u.UPwd == userPwd).FirstOrDefault();//对用户名密码进行过滤.
                    if (userInfo == null)
                    {
                        return(Content("no:用户名或密码错误!:" + FailCount.ToString()));
                    }
                    else
                    {
                        Session["userInfo"] = userInfo;//低配版本直接存Session
                        //string sessionId = Guid.NewGuid().ToString();//自己创建的SessionId,作为Memcache的key.
                        //Common.MemcacheHelper.Set(sessionId, Common.SerializerHelper.SerializerToString(userInfo));//将用户的信息存储到Memcache中。
                        //Response.Cookies["sessionId"].Value = sessionId;//然后将自创的sessionId以Cookie的形式返回到浏览器,存储到浏览器端的内存中。
                        //判断一下用户是否选择了记住我.
                        if (!string.IsNullOrEmpty(Request["checkMe"]))
                        {
                            HttpCookie cookie1 = new HttpCookie("cp1", userName);                                                        //用户名
                            HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.Md5String(Common.WebCommon.Md5String(userPwd))); //密码2次MD5加密,更安全
                            cookie1.Expires = DateTime.Now.AddDays(3);                                                                   //记住多少天
                            cookie2.Expires = DateTime.Now.AddDays(3);
                            Response.Cookies.Add(cookie1);
                            Response.Cookies.Add(cookie2);
                        }
                        SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[userName] = 0;
                        return(Content("ok:"));
                    }
                    #endregion
                }
            }
            else
            {
                SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[userName] = 1;
                #region  需要验证码,成功登陆需重置验证码
                string userPwd  = Request["LoginPwd"];
                var    userInfo = userInfoService.LoadEntities(u => u.UName == userName && u.UPwd == userPwd).FirstOrDefault();//对用户名密码进行过滤.
                if (userInfo == null)
                {
                    return(Content("no:用户名或密码错误!:" + "1"));
                }
                else
                {
                    Session["userInfo"] = userInfo;//低配版本直接存Session
                    //string sessionId = Guid.NewGuid().ToString();//自己创建的SessionId,作为Memcache的key.
                    //Common.MemcacheHelper.Set(sessionId, Common.SerializerHelper.SerializerToString(userInfo));//将用户的信息存储到Memcache中。
                    //Response.Cookies["sessionId"].Value = sessionId;//然后将自创的sessionId以Cookie的形式返回到浏览器,存储到浏览器端的内存中。
                    //判断一下用户是否选择了记住我.
                    if (!string.IsNullOrEmpty(Request["checkMe"]))
                    {
                        HttpCookie cookie1 = new HttpCookie("cp1", userName);                                                        //用户名
                        HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.Md5String(Common.WebCommon.Md5String(userPwd))); //密码2次MD5加密,更安全
                        cookie1.Expires = DateTime.Now.AddDays(3);                                                                   //记住多少天
                        cookie2.Expires = DateTime.Now.AddDays(3);
                        Response.Cookies.Add(cookie1);
                        Response.Cookies.Add(cookie2);
                    }
                    SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[userName] = 0;
                    return(Content("ok:"));
                }
                #endregion
            }
        }
コード例 #2
0
        public ActionResult login(string name, string password, bool isEmail, bool isPhoneNum, string thirdSessionKey, string vCode)
        {//验证码初次不显示功能,实现为加缓存,缓存每10分钟清空一次。如果加到数据库会增加压力
            if (!checkRequestHeader(Request))
            {
                return(Content("forbid!"));
            }
            DbContext Db = Yuruisoft.RS.Model.wxShoppingMall.wxShoppingMallDBFactory.CreateDbContext();
            haowanFamilyAccountInfo result = new haowanFamilyAccountInfo();

            if (SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache.ContainsKey(thirdSessionKey))
            {
                short FailCount = SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[thirdSessionKey];
                if (FailCount > 3)
                {
                    #region 需要验证码,成功登陆需重置验证码
                    string validateCode = Session["validateCode"] == null ? string.Empty : Session["validateCode"].ToString();
                    if (string.IsNullOrEmpty(validateCode))
                    {
                        return(Json(new
                        {
                            error = "VCODEWRONG",
                            failCount = FailCount
                        }));
                    }
                    Session["validateCode"] = null;
                    if (!vCode.Equals(validateCode, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(Json(new
                        {
                            error = "VCODEWRONG",
                            failCount = FailCount
                        }));
                    }
                    #region 1、判断邮件名
                    if (isEmail)
                    {
                        result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.email == name).FirstOrDefault();
                    }
                    #endregion
                    #region 2、判断电话号码
                    if (isPhoneNum)
                    {
                        var temp = long.Parse(name);
                        result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.phoneNumber == temp).FirstOrDefault();
                    }
                    #endregion
                    #region 3、判断账户名
                    if ((!isEmail) && (!isPhoneNum))
                    {
                        result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.account == name).FirstOrDefault();
                    }
                    #endregion
                    if (result == null)
                    {
                        return(Json(new
                        {
                            error = "NAMEWRONG",
                            failCount = FailCount
                        }));
                    }
                    if (result.password != password)
                    {
                        return(Json(new
                        {
                            error = "PASSWORDWRONG",
                            failCount = FailCount
                        }));
                    }
                    SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[thirdSessionKey] = 0;
                    var passwordMD5 = Common.WebCommon.Md5String(Common.WebCommon.Md5String(password));
                    return(Json(new
                    {
                        account = result.account,
                        email = result.email,
                        phoneNumber = result.phoneNumber,
                        password = passwordMD5
                    }));

                    #endregion
                }
                else
                {
                    SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[thirdSessionKey]++;
                    #region  需要验证码,成功登陆需重置验证码
                    #region 1、判断邮件名
                    if (isEmail)
                    {
                        result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.email == name).FirstOrDefault();
                    }
                    #endregion
                    #region 2、判断电话号码
                    if (isPhoneNum)
                    {
                        var temp = long.Parse(name);
                        result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.phoneNumber == temp).FirstOrDefault();
                    }
                    #endregion
                    #region 3、判断账户名
                    if ((!isEmail) && (!isPhoneNum))
                    {
                        result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.account == name).FirstOrDefault();
                    }
                    #endregion
                    if (result == null)
                    {
                        return(Json(new
                        {
                            error = "NAMEWRONG",
                            failCount = FailCount
                        }));
                    }
                    if (result.password != password)
                    {
                        return(Json(new
                        {
                            error = "PASSWORDWRONG",
                            failCount = FailCount
                        }));
                    }
                    SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[thirdSessionKey] = 0;
                    var passwordMD5 = Common.WebCommon.Md5String(Common.WebCommon.Md5String(password));
                    return(Json(new
                    {
                        account = result.account,
                        email = result.email,
                        phoneNumber = result.phoneNumber,
                        password = passwordMD5
                    }));

                    #endregion
                }
            }
            else//第一次登陆
            {
                SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[thirdSessionKey] = 1;
                #region  需要验证码,成功登陆需重置验证码
                #region 1、判断邮件名
                if (isEmail)
                {
                    result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.email == name).FirstOrDefault();
                }
                #endregion
                #region 2、判断电话号码
                if (isPhoneNum)
                {
                    var temp = long.Parse(name);
                    result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.phoneNumber == temp).FirstOrDefault();
                }
                #endregion
                #region 3、判断账户名
                if ((!isEmail) && (!isPhoneNum))
                {
                    result = Db.Set <haowanFamilyAccountInfo>().Where(c => c.account == name).FirstOrDefault();
                }
                #endregion
                if (result == null)
                {
                    return(Json(new
                    {
                        error = "NAMEWRONG",
                        failCount = 0
                    }));
                }
                if (result.password != password)
                {
                    return(Json(new
                    {
                        error = "PASSWORDWRONG",
                        failCount = 0
                    }));
                }
                SingleLogOnVcodeCache.GetLogOnVcodeCache().LogOnCache[thirdSessionKey] = 0;
                var passwordMD5 = Common.WebCommon.Md5String(Common.WebCommon.Md5String(password));
                return(Json(new
                {
                    account = result.account,
                    email = result.email,
                    phoneNumber = result.phoneNumber,
                    password = passwordMD5
                }));

                #endregion
            }
        }