public ActionResult Logon(LogonViewModel logonModel)
        {
            if (ModelState.IsValid) {
                UserDataViewModel userDataViewModel = userDataRepository.Logon(logonModel.UserID, logonModel.Password);
                if(Session["UserID"] != null)
                    Session.Add("UserID",userDataViewModel.UserID);
                if (userDataViewModel != null) {
                    if (userDataViewModel.PwdError >= 3) {
                        //
                        ModelState.AddModelError("", "密碼輸入錯誤超過三次,請聯絡管理者處理");
                    }
                    
                    //查詢token是否已註冊,若未註冊需先註冊
                    if (Session["Token"] != null && Session["DeviceType"] != null && Session["DeviceCode"] != null) {
                        DeviceViewModel d = new DeviceViewModel();
                        d.UserID = userDataViewModel.UserID;
                        d.DeviceToken = Session["Token"].ToString();
                        d.DeviceType = Int32.Parse(Session["DeviceType"].ToString());
                        d.DeviceCode = Session["DeviceCode"].ToString();

                        string ret = userDataRepository.AddDevice(d);                        
                    }
                    var now = DateTime.Now;
                    string roles = userDataViewModel.UserName + "," + userDataViewModel.RoleID + "," + userDataViewModel.RoleIDStr;

                    var ticket = new FormsAuthenticationTicket(
                                    version: 1,
                                    name: userDataViewModel.UserID.ToString().ToUpper(),
                                    issueDate: now,
                                    expiration: now.AddDays(15),
                                    isPersistent: true,
                                    userData: roles,
                                    cookiePath: FormsAuthentication.FormsCookiePath);

                    var encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    cookie.Expires = DateTime.Now.AddDays(15);

                    Response.Cookies.Add(cookie);

                    //return View(logonModel);
                    return RedirectToAction("MobileMain", "Mobile");
                }
                else {
                    ModelState.AddModelError("", "帳號或密碼錯誤");
                }
            }
            else {
                ModelState.AddModelError("", "輸入錯誤");
            }
            return View(logonModel);
        }
        public ActionResult Logon(LogonViewModel logonModel)
        {
            if (ModelState.IsValid)
            {
                UserDataViewModel userDataViewModel = userDataRepository.Logon(logonModel.UserID, logonModel.Password);
                if (userDataViewModel != null)
                {
                    if (userDataViewModel.PwdError >= 3)
                    {
                        //
                        ModelState.AddModelError("", "密碼輸入錯誤超過三次,請聯絡管理者處理");
                    }
                    var now = DateTime.Now;
                    string roles = userDataViewModel.UserName + "," + userDataViewModel.RoleID + "," + userDataViewModel.RoleIDStr;



                    var ticket = new FormsAuthenticationTicket(
                                    version: 1,
                                    name: userDataViewModel.UserID.ToString().ToUpper(),                                    
                                    issueDate: now,
                                    expiration: now.AddHours(8),
                                    isPersistent: true,
                                    userData: roles,
                                    cookiePath: FormsAuthentication.FormsCookiePath);

                    var encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    cookie.Expires = DateTime.Now.AddHours(8);
                    Response.Cookies.Add(cookie);
                    Session.Add("removeMenuButton", roleToMFucRespositiory.GetRemoveButtonName(userDataViewModel.RoleID));
                    

                    //return View(logonModel);
                    return RedirectToAction("MainPage", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "帳號或密碼錯誤");
                }
            }
            else
            {
                ModelState.AddModelError("", "輸入錯誤");
            }
            return View(logonModel);
            //return RedirectToAction("MainPage", "Home");
        }