Example #1
0
        public ActionResult Register(RegisterViewModel registerdata)
        {
            if (ModelState.IsValid)
            {
                //帳號是否已經存在
                if (memberservice.CheckAccountExisted(registerdata.Account))
                {
                    //密碼加密
                    registerdata.Password = memberservice.HashPassword(registerdata.Password);

                    //寫入會員資料表
                    int rowcount = memberservice.AddMember(registerdata);

                    if (rowcount != 1)
                    {
                        ViewBag.Error = "新增會員失敗";

                        return(View());
                    }

                    //查詢會員資料
                    MemberLoginModel logindata = memberservice.GetMemberLoginData(registerdata.Account, registerdata.Password);

                    //會員資料寫入Session
                    if (logindata != null)
                    {
                        //Session["Login"] = string.Format("{0}_{1}_{2}", logindata.Member_ID, logindata.NickName, logindata.Role);
                        //物件序列化成JSON
                        //Session[SessionManager.SessionKey.SessionKeyName.MemberLogin] = JsonConvert.SerializeObject(logindata);
                        //呼叫方法寫入Session
                        LoginSession relatedSession = new LoginSession();
                        relatedSession.WriteLoginSession(logindata);
                    }

                    if (Session[SessionManager.SessionKey.SessionKeyName.MemberLogin] != null)
                    {
                        //轉導頁面到留言板
                        return(RedirectToAction("GetMessage", "GuestBook"));
                    }
                }
            }
            ViewBag.Error = "已經有此帳號,請重新輸入帳號。";
            return(View());
        }