Exemplo n.º 1
0
        public ActionResult PostUpdateInfo(Models.UpdateInfoModels model)
        {
            Hub.Models.User user = AccountStrategy.GetAccount(new Guid(Session["User"].ToString()));
            ViewBag.StuNumber = user.StuNumber;
            ViewBag.Email = user.Email;
            ViewBag.NickName = user.NickName;
            ViewBag.Tel = user.Tel;
            ViewBag.Sex = user.Sex;
            ViewBag.TrueName = user.TrueName;

            if (!ModelState.IsValid)
            {
                return View("Index");
            }
            else
            {
                //从Session获取Guid
                Guid userid = new Guid(Session["User"].ToString());
                //原来的NickName
                string nickname = AccountStrategy.GetNickNameByUserID(userid);
                //如果更改了昵称并且被占用的话..
                if (!model.NickName.Equals(nickname) && AccountStrategy.NickNameRegistered(model.NickName))
                {
                    ModelState.AddModelError("NickName", "昵称被占用咯~");
                    return View("Index", model);
                }
                //更新用户信息
                Hub.Models.User user2 = new Hub.Models.User
                {
                    UserID = userid,
                    NickName = model.NickName,
                    Sex = model.Sex,
                    Tel = model.Tel,
                    TrueName = model.TrueName
                };
                if (!AccountStrategy.UpdateAccount(user2))
                {
                    return Content("<script>alert('你真的有改信息吗~');window.location.href='Index'</script>");
                }
                return Content("<script>alert('完善信息成功!');window.location.href='Index'</script>");
            }
        }
Exemplo n.º 2
0
        public ActionResult PostRegister(Models.RegisterModels model, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid)
            {
                return View("Register", model);
            }
            else
            {
                //验证
                if (AccountStrategy.EmailRegistered(model.Email))
                {
                    ModelState.AddModelError("Email", "邮箱已经被注册");
                    return View("Register", model);
                }
                if (AccountStrategy.StuNumberRegistered(model.StuNumber))
                {
                    ModelState.AddModelError("StuNumber", "学号已经被注册");
                    return View("Register", model);
                }
                Models.CheckCodeModels checkcode = Session["CheckCode"] as Models.CheckCodeModels;
                if (checkcode == null || !checkcode.ValidCheckCode(model.Email, model.CheckCode))
                {
                    ModelState.AddModelError("CheckCode", "验证码错误");
                    return View("Register", model);
                }
                if (!model.Password.Equals(model.EnsurePassord))
                {
                    ModelState.AddModelError("EnsurePassord", "密码不一致");
                    return View("Register", model);
                }
                if (AccountStrategy.NickNameRegistered(model.NickName))
                {
                    ModelState.AddModelError("NickName", "昵称被占用咯~");
                    return View("Register", model);
                }
                /*if (AuthentiationStrategy.ValidStuNumber(model.StuNumber, model.StuPassword) == 0)
                {
                    ModelState.AddModelError("StuPassword", "学号认证失败");
                    return View("Register", model);
                }*/

                //图片不超过5M
                if (file == null && file.ContentLength > 1024 * 1024 * 5)
                {
                    ModelState.AddModelError("", "请上传规定大小的图片");
                    return View("Register", model);
                }

                //验证通过后
                Session.Remove("CheckCode");
                //新用户插入数据库 session赋值
                Hub.Models.User user = new Hub.Models.User
                {
                    UserID = Guid.NewGuid(),
                    Email = model.Email,
                    Password = model.Password,
                    NickName = model.NickName,
                    StuNumber = model.StuNumber
                };

                string absolutePath = SiteConfig.SitePath;
                string path = GetPath(user.UserID.ToString(), "User");
                string fullPath = absolutePath + path;
                string url = path + DateTime.Now.Ticks + ".png";
                using (var stream = file.InputStream)
                {
                    Image img = Image.FromStream(stream);
                    var bmp = ResizeImg(img);
                    if (!System.IO.Directory.Exists(fullPath))
                        System.IO.Directory.CreateDirectory(fullPath);
                    bmp.Save(absolutePath+url, ImageFormat.Png);
                }
                user.Avatar = url;

                Session["User"] = AccountStrategy.CreateAccount(user);
                //token create
                string token = AuthentiationStrategy.CreateToken(model.Email, model.Password);
                string token_checkcode = AuthentiationStrategy.CreateCheckCode(Request.UserAgent, Request.UserHostAddress);
                AuthentiationStrategy.InsertTokenToDB(user.UserID, token, token_checkcode);

                //token cookie
                HttpCookie cookie = new HttpCookie("ghy_sso_token", token);
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now + TimeSpan.FromDays(30);
                Response.SetCookie(cookie);
            }
            return Redirect(SiteConfig.SiteUrl+"/Home/Index");
        }