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);
        }
        //紀錄APP使用者的設備資訊
        public String AddDevice(DeviceViewModel device)
        {
            string ret = "false";

            int ID = 0;

            ID = db.UserDevice.AsEnumerable().Select(x => x.DeviceID).DefaultIfEmpty(0).Max(x => x) + 1;
            if (db.UserDevice.Where(x => x.DeviceCode == device.DeviceCode).Count() == 0)
            {
                try
                {
                    UserDevice userDevice = new UserDevice();
                    userDevice.DeviceID = ID;
                    userDevice.DeviceToken = device.DeviceToken;
                    userDevice.DeviceType = device.DeviceType;
                    userDevice.UserID = device.UserID;
                    userDevice.DeviceCode = device.DeviceCode;

                    db.UserDevice.Add(userDevice);
                    db.SaveChanges();
                    ret = "true";
                }
                catch
                {
                }
            }
            else
            {
                try
                {
                    int Did = db.UserDevice.AsEnumerable().Where(x => x.DeviceCode == device.DeviceCode).Select(x => x.DeviceID).First();
                    UserDevice userDevice = db.UserDevice.Find(Did);
                    userDevice.DeviceToken = device.DeviceToken;
                    userDevice.UserID = device.UserID;
                    db.SaveChanges();
                    ret = "true";
                }
                catch
                {
                }

            }
            return ret;
        }