示例#1
0
        public ActionResult Index(string UName, string UPwd, string vcode)
        {
            string validateCode = Session["validateCode"]?.ToString();

            if (string.IsNullOrEmpty(validateCode))
            {
                return(Content("no:验证码错误"));
            }
            Session["validateCode"] = null;
            if (!validateCode.Equals(vcode, StringComparison.InvariantCultureIgnoreCase))
            {
                return(Content("no:验证码错误"));
            }
            var userInfo = userInfoService.LoadEntities(u => u.UName == UName && u.UPwd == UPwd).FirstOrDefault();

            if (userInfo == null)
            {
                return(Content("no:用户名或密码错误"));
            }
            //Session["userInfo"] = userInfo;
            string sessionID = Guid.NewGuid().ToString();

            //将用户信息存在memcache中,过期时间20分钟
            MemcacheHelper.Set(sessionID, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));
            Response.Cookies["sessionID"].Value = sessionID;
            return(Content("ok:验证通过"));
        }
        public JsonResult ForgetPasswordEmail(string email)
        {
            var data = new { code = 1, msg = "邮件发送失败,请重试。" };
            var user = userService.LoadEntity(u => u.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase));

            if (user != null)
            {
                var id          = Guid.NewGuid().ToString("N");
                var link        = GlobalConfig.UrlPrefix + $"/account/resetpassword?active={id}";
                var templetpath = Server.MapPath("~/Templates/RetrievePassword.txt");
                NameValueCollection collection = new NameValueCollection();
                var timespan = DateTime.Now.AddMinutes(10);
                collection.Add("ename", user.Nickname);
                collection.Add("link", link);
                collection.Add("expired", timespan.ToString("yyyy-MM-dd HH:mm:ss"));
                var body = TemplateHelper.BuildByFile(templetpath, collection);
                if (EmailHelper.Send(user.Email, "找回您的账户密码", body))
                {
                    MemcacheHelper.Set(id, SerializerHelper.SerializeToString(user), timespan);
                    data = new { code = 0, msg = "重置密码链接已经发送到您的邮箱中了,请注意查收。" };
                }
            }

            return(Json(data));
        }
        public JsonResult ResetPassword(string active, ResetPwd resetPwd)
        {
            var data = new { code = 1, msg = "修改失败" };

            if (!string.IsNullOrEmpty(active) && ModelState.IsValid)
            {
                if (TempData[Keys.ValidCode] == null || !TempData[Keys.ValidCode].ToString().Equals(resetPwd.Code, StringComparison.CurrentCultureIgnoreCase))
                {
                    data = new { code = 1, msg = "验证码错误" };
                    //Session[Keys.ValidCode] = null;
                    return(Json(data));
                }
                var obj = MemcacheHelper.Get(active);
                if (obj != null)
                {
                    var user = SerializerHelper.DeserializeToObject <User>(obj.ToString());
                    if (user != null)
                    {
                        user.Login_Password = resetPwd.Password.Md5_32();
                        userService.EditEntity(user);
                        if (userService.SaveChanges())
                        {
                            data = new { code = 0, msg = "修改成功,请牢记新密码。" };
                            MemcacheHelper.Set(active, null, DateTime.Now.AddHours(-1));
                        }
                    }
                }
            }
            return(Json(data));
        }
示例#4
0
        public ActionResult UserLogin()
        {
            string uName = Request["LoginCode"];
            string uPwd  = Request["LoginPwd"];
            string code  = Request["vCode"];

            if (code == "###")
            {
            }
            else if (Session["ValidateCode"] == null || !code.Equals(Session["ValidateCode"]))
            {
                return(Content("error:验证码错误"));
            }
            var userInfo = UserInfoService.LoadEntites(o => o.UName == uName).FirstOrDefault();

            if (userInfo == null)
            {
                return(Content("error:账户不存在"));
            }
            if (userInfo.Pwd != uPwd)
            {
                return(Content("error:密码错误"));
            }
            string loginID = Guid.NewGuid().ToString();

            // 将用户的信息存到Session中
            Session["UserInfo"] = userInfo;
            // 将用户的信息存到Cookie中
            Response.Cookies["LoginID"].Value = loginID;
            // 将用户的信息存到Memcache中
            MemcacheHelper.Set(loginID, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));
            return(Content("ok:登陆成功"));
        }
示例#5
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            //if (Session["userInfo"] == null)
            //{
            //    filterContext.HttpContext.Response.Redirect("/Login/Index");
            //}
            bool isScuess = false;

            if (Request.Cookies["sessionID"] != null)
            {
                string sessionID = Request.Cookies["sessionID"].Value;
                object obj       = MemcacheHelper.Get(sessionID);
                if (obj != null)
                {
                    UserInfo userInfo = SerializeHelper.DeserializeToObject <UserInfo>(obj.ToString());
                    LoginUser = userInfo;
                    //模拟滑动过期时间
                    MemcacheHelper.Set(sessionID, obj, DateTime.Now.AddMinutes(20));
                    isScuess = true;
                }
            }
            if (!isScuess)
            {
                filterContext.Result = Redirect("/Login/Index");
            }
        }
示例#6
0
        public ActionResult UserLogin()
        {
            if (Response.Cookies["sessionId"].Value != null)
            {
                return(Content("ok:登录成功"));
            }
            else
            {
                string validateCode = Session["validateCode"] != null ? Session["validateCode"].ToString() : string.Empty;

                if (string.IsNullOrEmpty(validateCode))
                {
                    return(Content("no:验证码错误!!"));
                }
                Session["validateCode"] = null;
                string txtCode = Request["vCode"];
                if (!validateCode.Equals(txtCode, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(Content("no:验证码错误!!"));
                }
                string userName = Request["LoginCode"];
                string userPwd  = Request["LoginPwd"];
                bool   check    = Request["Check"] == "checded" ? true : false;
                var    userInfo = UserInfoService.LoadEntities(u => u.UName == userName && u.UPwd == userPwd).FirstOrDefault();             //根据用户名找用户
                if (userInfo != null)
                {
                    //Session["userInfo"] = userInfo;
                    //这里用Memcache代替Session是因为一旦用户访问量大了一台机器无法满足 就要建立多台机器处理用户访问 如果用seeion保存用户数据 只能保存在一台机器中 如果用户下次请求被其他机器接收 会检测不到用户数据 用户有得重新登录 而Membercache中数据可以在多台机器中共享 (这就是分布式缓存的一种应用)
                    //产生一个GUID值作为Memache的键.
                    string sessionId = Guid.NewGuid().ToString();
                    //这里报错了,可能是生成UserInfo的T4模板没有修改加上[JsonIgnore] 序列号不了后面几个复杂类型数据
                    MemcacheHelper.Set(sessionId, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));                    //将登录用户信息存储到Memcache中。

                    #region 自动登录
                    //Response.Cookies["sessionId"].Value = sessionId;//将Memcache的key以Cookie的形式返回给浏览器。
                    if (check)
                    {
                        HttpCookie cookie = new HttpCookie("sessionId");
                        cookie.Value   = sessionId;
                        cookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        HttpCookie cookie = new HttpCookie("sessionId");
                        cookie.Value = sessionId;
                        Response.Cookies.Add(cookie);
                    }
                    #endregion
                    return(Content("ok:登录成功"));
                }
                else
                {
                    return(Content("no:登录失败"));
                }
            }
        }
示例#7
0
        public ActionResult GetVCode()
        {
            ValidateCode vliateCode = new ValidateCode();
            string       code       = vliateCode.CreateValidateCode(4);//产生验证码
            string       sId        = MemcacheHelper.Set(code, DateTime.Now.AddMinutes(20));

            Response.Cookies["vCodeSID"].Value = sId;
            byte[] buffer = vliateCode.CreateValidateGraphic(code);
            return(File(buffer, "image/jpeg"));
        }
示例#8
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            string strController = filterContext.RouteData.Values["controller"].ToString();
            string strAction     = filterContext.RouteData.Values["action"].ToString();

            if (strController.Equals("Login") || strController.Equals("Error"))
            {
                return;     // 如果访问的是 Login 就直接放回
            }
            // 校验用户登录
            if (filterContext.HttpContext.Request.Cookies["LoginID"] == null)
            {
                filterContext.HttpContext.Response.Redirect("/Login/Index");
                return;
            }
            string loginId = filterContext.HttpContext.Request.Cookies["LoginID"].Value;

            if (string.IsNullOrEmpty(loginId))
            {
                // 留一个后门,请求的 Contorller 是Articles 或 Search ,则忽略登录验证
                if (strController.Equals("Articles") || strController.Equals("Search"))
                {
                    return;
                }
                filterContext.HttpContext.Response.Redirect("/Login/Index");
                return;
            }
            object loginUser = MemcacheHelper.Get(loginId); //SerializeHelper

            if (loginUser == null)
            {
                filterContext.HttpContext.Response.Redirect("/Login/Index");
                return;
            }
            UserInfo userInfo = SerializeHelper.DeserializeToObject <UserInfo>(loginUser.ToString());

            if (userInfo == null)
            {
                filterContext.HttpContext.Response.Redirect("/Login/Index");
                return;
            }
            MemcacheHelper.Set(loginId, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));  // 重新设置过期时间 即 平滑时间
            // 校验用户权限
            IApplicationContext ctx               = ContextRegistry.GetContext();
            IUserInfoService    userInfoService   = ctx.GetObject <UserInfoService>("UserInfoService");
            IActionInfoService  actionInfoService = ctx.GetObject <ActionInfoService>("ActionInfoService");

            userInfo = userInfoService.LoadEntites(o => o.ID.Equals(userInfo.ID)).FirstOrDefault();
            BaseController.LoginUser = userInfo;
            if (!actionInfoService.ValidateUserAction(userInfo, filterContext.HttpContext.Request))
            {
                filterContext.HttpContext.Response.Redirect("/Error/Index/?msg=" + "无访问权限");
            }
        }
示例#9
0
        public string GetStringValue(string key)
        {
            var value = MemcacheHelper.Get(key)?.ToString();

            if (value == null)
            {
                value = settingRepository.LoadEntity(s => s.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase))?.Value;
                MemcacheHelper.Set(key, value, DateTime.Now.AddHours(1));
            }
            return(value);
        }
        //Mencache缓存测试
        public ActionResult Index()
        {
            var obj = MemcacheHelper.Get("zhangyi");

            if (obj == null)
            {
                MemcacheHelper.Set("zhangyi", "我的名字叫张毅");
            }



            return(View());
        }
示例#11
0
        /// <summary>
        /// 完成用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult UserLogin()
        {
            //校验验证码
            string validateCode = Session["vcode"] != null ? Session["vcode"].ToString() : string.Empty;

            if (string.IsNullOrEmpty(validateCode))
            {
                return(Content("no:验证码错误!!"));
            }
            Session["vcode"] = null;
            string userCode = Request["vCode"];

            if (!validateCode.Equals(userCode, StringComparison.InvariantCultureIgnoreCase))
            {
                return(Content("no:验证码错误!!"));
            }
            string userName = Request["LoginCode"];
            string userPwd  = Request["LoginPwd"];

            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(userPwd))
            {
                string uPwd     = Common.WebCommon.GetMd5String(Common.WebCommon.GetMd5String(userPwd));               //密码两次MD5加密
                var    userInfo = UserInfoBLL.LoadEntity(u => u.UName == userName && u.UPwd == uPwd).FirstOrDefault(); //校验用户名密码。
                if (userInfo != null)
                {
                    //Session["userInfo"] = userInfo;
                    string sessionId = Guid.NewGuid().ToString();
                    MemcacheHelper.Set(sessionId, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));
                    Response.Cookies["sessionId"].Value = sessionId;
                    //判断复选框是否被选中.
                    if (!string.IsNullOrEmpty(Request["autoLogin"]))
                    {
                        HttpCookie cookie1 = new HttpCookie("cp1", userName);
                        HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.GetMd5String(Common.WebCommon.GetMd5String(userPwd)));
                        cookie1.Expires = DateTime.Now.AddDays(7);
                        cookie2.Expires = DateTime.Now.AddDays(7);
                        Response.Cookies.Add(cookie1);
                        Response.Cookies.Add(cookie2);
                    }
                    return(Content("ok:登录成功!!"));
                }
                else
                {
                    return(Content("no:用户名密码错误!!"));
                }
            }
            else
            {
                return(Content("no:用户名密码不能为空!!"));
            }
        }
示例#12
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request  = filterContext.HttpContext.Request;
            var response = filterContext.HttpContext.Response;

            // 判断是否跳过登录验证
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), false))
            {
                return;
            }
            if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), false))
            {
                return;
            }
            var cookie = request.Cookies[Keys.SessionId];

            if (cookie != null)
            {
                var sessionid = request.Cookies[Keys.SessionId]?.Value;
                if (sessionid != null)
                {
                    var obj = MemcacheHelper.Get(sessionid);
                    if (obj != null)
                    {
                        var account = SerializerHelper.DeserializeToObject <Account>(obj.ToString());
                        if (account != null)
                        {
                            // 滑动过期时间
                            cookie.Expires  = DateTime.Now.AddHours(1);
                            cookie.HttpOnly = true;
                            response.Cookies.Add(cookie);
                            MemcacheHelper.Set(sessionid, obj, DateTime.Now.AddHours(1));
                            Account = account;
                            return;
                        }
                    }
                }
            }

            if (request.IsAjaxRequest())
            {
                filterContext.Result = new AjaxUnauthorizedResult();
                return;
            }
            response.StatusCode = 401;
            var from = filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Url.ToString());

            filterContext.Result = new RedirectResult($"/account/signin?returnUrl={from}");
            base.OnActionExecuting(filterContext);
        }
示例#13
0
        [ValidateAntiForgeryToken]//防伪造令牌来避免CSRF攻击
        public ActionResult Index(LogOnModel model)
        {
            #region 验证码验证

            if (GetSession("ValidateCode") != null && model.ValidateCode != null && model.ValidateCode.ToLower() != GetSession("ValidateCode").ToString())
            {
                ModelState.AddModelError("Error_PersonLogin", "验证码错误!");
                return(View());
            }
            SetSession("ValidateCode", null);
            #endregion

            if (ModelState.IsValid)
            {
                SMUSERTB person = SMUSERTBService.ValidateUser(model.PersonName, Encrypt.DecodeText(model.Password));
                if (person != null) //登录成功
                {
                    Account account = person.ToAccount();

                    string sessionId = Guid.NewGuid().ToString();//作为Memcache的key
                    try
                    {
                        MemcacheHelper.Set(sessionId, Common.SerializerHelper.SerializeToString(account), DateTime.Now.AddMinutes(20));//使用Memcache代替Session解决数据在不同Web服务器之间共享的问题。
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    //Response.Cookies["sessionId"].Value = sessionId;//将Memcache的key以cookie的形式返回到浏览器端的内存中,当用户再次请求其它的页面请求报文中会以Cookie将该值再次发送服务端。
                    SetCookies("sessionId", sessionId);
                    if (model.RememberMe)
                    {
//                        HttpCookie ckUid = new HttpCookie("ckUid", model.PersonName);
//                        HttpCookie ckPwd = new HttpCookie("ckPwd", Encrypt.DecodeText(model.Password));
//                        ckUid.Expires = DateTime.Now.AddDays(3);
//                        ckPwd.Expires = DateTime.Now.AddDays(3);
//                        Response.Cookies["sessionId"].Expires = DateTime.Now.AddDays(3);
//                        Response.Cookies.Add(ckUid);
//                        Response.Cookies.Add(ckPwd);
                        SetCookies("ckUid", model.PersonName, 60 * 60 * 24 * 3);
                        SetCookies("ckPwd", Encrypt.DecodeText(model.Password), 60 * 60 * 24 * 3);
                    }
                    return(RedirectToAction("Index", "Home"));
                }
            }
            ModelState.AddModelError("Error_PersonLogin", "用户名或者密码出错。");
            return(View());
        }
示例#14
0
        public ActionResult Index()
        {
            HttpCookie httpCookie = Request.Cookies.Get("userInfo");
            UserInfo   userInfo;

            if (httpCookie != null)
            {
                userInfo = SerializeHelper.DeserializeToObject <UserInfo>(httpCookie.Value);
                userInfo = userInfoService.LoadEntities(u => (u.ID == userInfo.ID || u.UName == userInfo.UName) && u.UPwd == userInfo.UPwd).FirstOrDefault();
                string userInfoSID = MemcacheHelper.Set(SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));
                Response.Cookies["userInfoSID"].Value = userInfoSID;
                //  Response.Cookies["userInfoSID"].Expires = DateTime.Now.AddDays(1);
                return(Redirect("/Home/Index"));
            }
            return(View());
        }
示例#15
0
 void ValidatedUser(Account account)
 {
     try
     {
         var sessionid = Guid.NewGuid().ToString();
         MemcacheHelper.Set(sessionid, SerializerHelper.SerializeToString(account), DateTime.Now.AddHours(1));
         Response.Cookies[Keys.SessionId].Value    = sessionid;
         Response.Cookies[Keys.SessionId].Expires  = DateTime.Now.AddHours(1);
         Response.Cookies[Keys.SessionId].HttpOnly = true;
         //Response.Cookies[Keys.UserId].Value = account.User_Id.ToString();
     }
     catch (Exception e)
     {
         throw e;
     }
 }
示例#16
0
        /// <summary>
        /// 完成用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult UserLogin()
        {
            ResultCodeEnum resultCodeEnum = ResultCodeEnum.Failure;
            string         message        = string.Empty;
            var            sessionCode    = Session["validateCode"];
            string         validateCode   = sessionCode != null?sessionCode.ToString() : string.Empty;

            if (string.IsNullOrEmpty(validateCode))
            {
                message = "验证码错误";
            }
            else
            {
                Session["validateCode"] = null;
                string txtCode = Request["vCode"];
                if (!validateCode.Equals(txtCode, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = "验证码错误";
                }
                else
                {
                    string account  = Request["LoginCode"];
                    string userPwd  = Request["LoginPwd"];
                    var    userInfo = UserInfoService.LoadEntities(o => o.Account == account && o.UserPassword == userPwd).FirstOrDefault();
                    if (userInfo != null)
                    {
                        // Session["userInfo"] = userInfo;
                        //产生一个GUID值作为Memache的键.
                        //  System.Web.Script.Serialization.JavaScriptSerializer
                        string sessionId = Guid.NewGuid().ToString();
                        //将登录用户信息存储到Memcache中。
                        MemcacheHelper.Set(sessionId, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));
                        //将Memcache的key以Cookie的形式返回给浏览器。
                        Response.Cookies["sessionId"].Value = sessionId;
                        resultCodeEnum = ResultCodeEnum.Success;
                        message        = "登录成功";
                    }
                    else
                    {
                        message = "登录失败";
                    }
                }
            }
            ResultModel <string> resultModel = new ResultModel <string>(resultCodeEnum, message);

            return(Json(resultModel, JsonRequestBehavior.AllowGet));
        }
示例#17
0
        public ActionResult UserLogin(Models.LoginForm from)
        {
            string vcodeSID = Request.Cookies["vCodeSID"].Value.ToString();

            Response.Cookies.Remove("vCodeSID");
            //获取正确验证码
            string vode = MemcacheHelper.Get(vcodeSID).ToString();
            //根据用户名取得用户信息;
            int id = 0;

            if ((from.code == null || from.code == "") || from.code.Trim() != vode)
            {
                return(Content("no:验证码错误!"));
            }

            Model.UserInfo userInfo = userInfoService.LoadEntities(u => u.UName == from.user).FirstOrDefault();
            if (userInfo == null)
            {
                if (int.TryParse(from.user, out id))
                {
                    userInfo = userInfoService.LoadEntities(u => u.ID == id).FirstOrDefault();
                    if (userInfo == null)
                    {
                        return(Content("no:用户名错误"));
                    }
                }
            }
            if (from.pwd != userInfo.UPwd)
            {
                return(Content("no:密码错误"));
            }
            if (from.checkpwd != null)
            {
                Response.Cookies["userInfo"].Value   = SerializeHelper.SerializeToString(userInfo);//登陆成功将userinfo写入cookies
                Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
            }
            else
            {
                Response.Cookies.Remove("userInfo");
            }
            string userInfoSID = MemcacheHelper.Set(SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));

            Response.Cookies["userInfoSID"].Value = userInfoSID;
            // Response.Cookies["userInfoSID"].Expires = DateTime.Now.AddDays(1);
            return(Content("ok:登陆成功"));
        }
示例#18
0
        public List <Favorites> CurrentUserFavorites(long userId)
        {
            List <Favorites> favorites = null;
            var obj = MemcacheHelper.Get(userId.ToString());

            if (obj != null)
            {
                favorites = SerializerHelper.DeserializeToObject <List <Favorites> >(obj.ToString());
            }
            else
            {
                favorites = favoritesRepository.LoadEntities(f => f.User_Id == userId)?.ToList();
                MemcacheHelper.Set(userId.ToString(), SerializerHelper.SerializeToString(favorites), DateTime.Now.AddMinutes(10));
            }

            return(favorites);
        }
        public List <FriendLinks> GetFriendLinks()
        {
            List <FriendLinks> friendLinks = null;

            var obj = MemcacheHelper.Get(Keys.FriendLinks);

            if (obj != null)
            {
                friendLinks = SerializerHelper.DeserializeToObject <List <FriendLinks> >(obj.ToString());
            }
            if (friendLinks == null)
            {
                friendLinks = friendLinksRepository.LoadEntities(f => true).OrderBy(f => f.Sort)?.ToList();
                MemcacheHelper.Set(Keys.FriendLinks, SerializerHelper.SerializeToString(friendLinks), DateTime.Now.AddHours(24));
            }
            return(friendLinks);
        }
示例#20
0
        public List <Article_Category> GetCategories()
        {
            List <Article_Category> categories = null;
            var obj = MemcacheHelper.Get(Keys.Categories);

            if (obj != null)
            {
                categories = SerializerHelper.DeserializeToObject <List <Article_Category> >(obj.ToString());
            }
            if (categories == null)
            {
                categories = categoryRepository.LoadEntities(c => true).OrderBy(c => c.Sort).ToList();
                MemcacheHelper.Set(Keys.Categories, SerializerHelper.SerializeToString(categories));
            }

            return(categories);
        }
示例#21
0
        public JsonResult ChangePwd(string oldPwd, string newPwd)
        {
            string msg  = string.Empty;
            var    user = userService.ChangePassword(Account.User_Id, oldPwd, newPwd, out msg);

            var data = new { code = 1, msg = msg };

            if (user != null)
            {
                var sessionid = Guid.NewGuid().ToString();
                MemcacheHelper.Set(sessionid, SerializerHelper.SerializeToString(user.ToAccount()), DateTime.Now.AddHours(1));
                Response.Cookies[Keys.SessionId].Value   = sessionid;
                Response.Cookies[Keys.SessionId].Expires = DateTime.Now.AddHours(1);
                data = new { code = 0, msg = "修改成功!" };
            }

            return(Json(data));
        }
示例#22
0
        public ActionResult Valid(string confirmatio)
        {
            ViewBag.Model = confirmatio;
            var email = Session["_email"]?.ToString();

            if (!string.IsNullOrEmpty(email) && string.IsNullOrEmpty(confirmatio))
            {
                var id   = Guid.NewGuid().ToString("N");
                var user = userService.LoadEntity(u => u.Email.Equals(email));
                if (SendValidEmail(email, id) && user != null)
                {
                    MemcacheHelper.Set(id, SerializerHelper.SerializeToString(user), DateTime.Now.AddHours(1));
                }
                ViewBag.Model = id;
                ViewBag.Email = email;
            }
            return(View());
        }
示例#23
0
        /// <summary>
        /// 是否为禁用词
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool IsForbidWord(string msg)
        {
            List <string> list = null;
            object        obj  = MemcacheHelper.Get("forbidWord");

            if (obj == null)
            {
                list = this.DbSession.SensitiveWordDAL.LoadEntity(a => a.IsForbid == true).Select(a => a.WordPattern).ToList();
                string str = SerializeHelper.SerializeToString(list);
                MemcacheHelper.Set("forbidWord", str);
            }
            else
            {
                list = SerializeHelper.DeSerializeToObj <List <string> >(obj.ToString());
            }
            string regex = string.Join("|", list.ToArray());

            return(Regex.IsMatch(msg, regex));
        }
示例#24
0
        public ActionResult UserLogin()
        {
            string validateCode     = Session["validateCode"] != null ? Session["validateCode"].ToString() : string.Empty;
            string userValidateCode = Request["vCode"];

            if (string.IsNullOrEmpty(validateCode))
            {
                return(Content("no:验证码错误"));
            }
            Session["validateCode"] = null;
            if (!validateCode.Equals(userValidateCode, StringComparison.InvariantCultureIgnoreCase))
            {
                return(Content("no:验证码错误"));
            }
            string userName = Request["LoginCode"];
            string userPwd  = Request["LoginPwd"];
            var    userInfo = UserInfoService.LoadEntities(u => u.UName == userName).FirstOrDefault();//根据用户名找用户

            if (userInfo != null)
            {
                if (userInfo.UPwd == userPwd)
                {
                    Session["userInfo"] = userInfo;
                    //有个问题 存到session的问题 如果部署到多台服务器上
                    //产生一个GUID值作为Memache的键.
                    string sesId = Guid.NewGuid().ToString();
                    MemcacheHelper.Set(sesId, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20)); //将登陆信息存储到memcache中
                    Response.Cookies["sesId"].Value = sesId;                                                             //将key以cookie的形式返回给浏览器



                    return(Content("ok:登录成功"));
                }
                else
                {
                    return(Content("no:密码错误,请重新输入"));
                }
            }
            else
            {
                return(Content("no:用户名不存在"));
            }
        }
示例#25
0
        /// <summary>
        /// 测试memcached
        /// </summary>
        /// <returns></returns>
        public ActionResult Memcached()
        {
            string str1 = string.Empty;
            string str2 = string.Empty;

            //if (MemcacheHelper.Delete("test"))
            //{
            //    str1 = "原来的给我删了!";
            //}
            str2 = "空滴";
            if (MemcacheHelper.Get("test") != null)
            {
                str2 = MemcacheHelper.Get("test").ToString();
            }
            else
            {
                MemcacheHelper.Set("test", "我有值啦~", DateTime.Now.AddMinutes(20));
            }
            return(Content(str1 + str2));
        }
示例#26
0
        public JsonResult ReSendValidEmail()
        {
            var data  = new { code = 1, msg = "发送失败!" };
            var email = Session["_email"]?.ToString();

            if (!string.IsNullOrEmpty(email))
            {
                var userinfo = userService.LoadEntity(u => u.Email.Equals(email));
                if (userinfo != null)
                {
                    var id = Guid.NewGuid().ToString("N");
                    if (SendValidEmail(email, id))
                    {
                        MemcacheHelper.Set(id, SerializerHelper.SerializeToString(userinfo), DateTime.Now.AddHours(1));
                        data = new { code = 0, msg = "邮件发送成功!" };
                    }
                }
            }
            return(Json(data));
        }
示例#27
0
        public string ReplaceWord(string msg)
        {
            List <SensitiveWord> list = null;
            object obj = MemcacheHelper.Get("replaceWord");

            if (obj == null)
            {
                list = this.DbSession.SensitiveWordDAL.LoadEntity(a => !a.IsMod && !a.IsForbid).ToList();
                string str = SerializeHelper.SerializeToString(list);
                MemcacheHelper.Set("replaceWord", str);
            }
            else
            {
                list = SerializeHelper.DeSerializeToObj <List <SensitiveWord> >(obj.ToString());
            }
            foreach (SensitiveWord item in list)
            {
                msg = msg.Replace(item.WordPattern, item.ReplaceWord);
            }
            return(msg);
        }
示例#28
0
 // GET: Login
 public ActionResult Index()
 {
     //检查Session是否存在
     //if (Session["UserInfo"] == null)
     if (Request["sessionID"] == null)
     {
         //检查Cookie储存的用户信息
         if (Request.Cookies["UserName"] != null)
         {
             string userName = Request.Cookies["UserName"].Value;
             //IOC
             IApplicationContext ctx         = ContextRegistry.GetContext();
             IUserInfoBll        userInfoBll = (IUserInfoBll)ctx.GetObject("UserInfoBll");
             UserInfo            userInfo    = userInfoBll.LoadEntities(u => u.UName == userName).FirstOrDefault();
             if (userInfo != null)
             {
                 if (WebCommon.ValidateUserInfoCookie(userInfo))
                 {
                     //信息正确 跳转主界面
                     return(Redirect(Url.Action("Index", "Home")));
                 }
             }
         }
     }
     else
     {
         string sessionID = Request.Cookies["sessionID"].Value;
         //获取Memcache中的数据
         object obj = MemcacheHelper.Get(sessionID);
         if (obj != null)
         {
             //反序列化存储在Memcache的用户
             UserInfo userInfo = SerializeHelper.DeSerializeToT <UserInfo>(obj.ToString());
             //模拟滑动过期时间。
             MemcacheHelper.Set(sessionID, obj, DateTime.Now.AddMinutes(20));
             return(Redirect(Url.Action("Index", "Home")));
         }
     }
     return(View());
 }
示例#29
0
        //注册处理
        public ActionResult UserReg()
        {
            var result = "no";

            if (Request["regId"] != null && Request["regPwd"] != null && Request["ifyCode"] != null && Request["ifyCode"].ToLower().Equals(Session["RegValidCode"].ToString(), StringComparison.InvariantCultureIgnoreCase))
            {
                UserInfo userinfo = new UserInfo
                {
                    Username   = Request["regId"],
                    Password   = CommonHelper.GetMD5String(Request["regPwd"] + CommonHelper.GetPasswordSalt()),
                    ErrorCount = 0,
                    IsDeleted  = 0,
                    AddTime    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    SubBy      = 19,
                    RoleInfo   = new List <RoleInfo> {
                        RoleInfoBll.GetById(6)
                    }
                };
                if (UserInfoBll.Add(userinfo))
                {
                    //Session["UserInfo"] = userinfo.Username;
                    string key   = Guid.NewGuid().ToString();
                    string value = SerializeHelper.SerializeToString(new UserLoginViewModel
                    {
                        UserId   = userinfo.UserId,
                        UserName = userinfo.Username,
                        UserPwd  = Request["regPwd"]
                    });
                    HttpCookie cookie = new HttpCookie("LData", key);
                    cookie.Path = "/";
                    MemcacheHelper.Set(key, value, DateTime.Now.AddDays(7));
                    Response.Cookies.Add(cookie);
                    result = "ok";
                }
            }

            return(Content(result));
        }
示例#30
0
        public ActionResult LoginCheck()
        {
            //获取验证码
            String validateCode = Session["validateCode"] != null ? Session["validateCode"].ToString() : string.Empty;

            //清空session
            Session["validateCode"] = null;
            if (String.IsNullOrEmpty(validateCode))
            {
                return(Content("验证码生成错误!"));
            }
            //用户输入的验证码
            string code = Request["vCode"];

            //判断验证码是否正确;
            if (code.Equals(validateCode, StringComparison.InvariantCultureIgnoreCase))//忽略大小写**
            {
                string txtUName = Request["LoginCode"];
                string TxtUPwd  = Request["LoginPwd"];
                var    user     = userInfoService.LoadEntities(u => u.UName == txtUName && u.UPwd == TxtUPwd).FirstOrDefault();
                if (user != null)
                {
                    //Response.SetCookie("userInfo");

                    string sessionId = Guid.NewGuid().ToString();
                    MemcacheHelper.Set(sessionId, SerializeHelper.SerializeToString(user), DateTime.Now.AddMinutes(20));
                    Response.Cookies["sessionId"].Value = sessionId;                     //将Memcache的key以Cookie的形式返回给浏览器。
                                                                                         //也就是说下一次只要浏览器拿着cookie就能打开memcache取出userinfo对象
                    Response.Cookies["sessionId"].Expires = DateTime.Now.AddMinutes(20); //如果不设置过期时间的话,关闭浏览器,cookies就会被清除
                    return(Content("ok"));
                }
                return(Content("no"));
            }
            else
            {
                return(Content("验证码错误!"));
            }
        }