//遊戲(User端)login public ActionResult GameUserLoginView(string LoginAccount, string numberid) { SDuser user = userdb.SDuser.Where(x => x.numberid == numberid).FirstOrDefault(); if (user != null) { Wedding_UserInfo wedding_user = userdb.Wedding_UserInfo.Where(x => x.username == LoginAccount).FirstOrDefault(); if (wedding_user == null) { //存入db Wedding_UserInfo userinfo = new Wedding_UserInfo() { username = LoginAccount, weddingname = user.adaccount, }; userdb.Wedding_UserInfo.Add(userinfo); userdb.SaveChanges(); //存入Cookies UserDataHandler.LoginWeddingSaveToCookies(userinfo); return(RedirectToAction("GameUserView", "Wedding")); } else { ViewBag.error = "已經有人使用,請重新輸入!!"; } } return(View()); }
//Cookies for wedding public static void LoginWeddingSaveToCookies(Wedding_UserInfo user) { FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( version: 1, name: user.username, issueDate: DateTime.Now, expiration: DateTime.Now.AddHours(10), isPersistent: false, userData: user.weddingname,//UserData用來儲存使用者編號 cookiePath: FormsAuthentication.FormsCookiePath ); string encryptTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket); cookie.HttpOnly = true; cookie.Expires = ticket.Expiration; HttpContext.Current.Response.Cookies.Add(cookie); }