public ActionResult Login(MembersLoginViewModel LoginMember) { //Service string ValidateStr = membersService.LoginCheck(LoginMember.Account, LoginMember.Password); //判斷驗證後結果是否錯誤訊息 if (String.IsNullOrEmpty(ValidateStr)) { string RoleData = membersService.GetRole(LoginMember.Account); JwtService jwtService = new JwtService(); //設定JWT //從web.Config string cookieName = WebConfigurationManager.AppSettings["CookieName"].ToString(); string Token = jwtService.GenerateToke(LoginMember.Account, RoleData); //產生一個cookie HttpCookie cookie = new HttpCookie(cookieName); //設應單值 cookie.Value = Server.UrlEncode(Token); //寫到用戶端 Response.Cookies.Add(cookie); //設定cookie權限 Response.Cookies[cookieName].Expires = DateTime.Now.AddMinutes(Convert.ToInt32(WebConfigurationManager.AppSettings["ExpireMinutes"])); return(RedirectToAction("Index", "Guestbooks")); } else { ModelState.AddModelError("", ValidateStr); return(View(LoginMember)); } }
public ActionResult Login(MembersLoginViewModel LoginMember) { string ValidateStr = membersService.LoginCheck(LoginMember.Account, LoginMember.Password); if (String.IsNullOrEmpty(ValidateStr)) { //無錯誤訊息,則登入 //先藉由Service取得登入者角色資料 string RoleData = membersService.GetRole(LoginMember.Account); //設定JWT JwtService jwtService = new JwtService(); //從Web.Config撈出資料 //Coolie名稱 string cookieName = WebConfigurationManager.AppSettings["CookieName"].ToString(); string Token = jwtService.GenerateToken(LoginMember.Account, RoleData); //產生一個Cookie HttpCookie cookie = new HttpCookie(cookieName); //設定單值 cookie.Value = Server.UrlEncode(Token); //寫到用戶端 Response.Cookies.Add(cookie); //設定cookie期限 Response.Cookies[cookieName].Expires = DateTime.Now.AddMinutes(Convert.ToInt32(WebConfigurationManager.AppSettings["ExpireMinutes"])); return(RedirectToAction("Index", "Home")); } else { //有驗證錯誤信息,加入頁面模型中 ModelState.AddModelError("", ValidateStr); return(View(LoginMember)); } }
[HttpPost] //設定此Action只接受頁面POST資料傳入 public ActionResult Login(MembersLoginViewModel LoginMember) { //使用Service裡的方法來驗證登入的帳號密碼 string ValidateStr = membersService.LoginCheck(LoginMember.Account, LoginMember.Password); //判斷驗證後結果是否有錯誤訊息 if (String.IsNullOrEmpty(ValidateStr)) { //無錯誤訊息,則登入 //先清空Session HttpContext.Session.Clear(); //取得購物車保存 string Cart = cartService.GetCartSave(LoginMember.Account); //判斷是否有保存,若有則存入Session if (Cart != null) { HttpContext.Session["Cart"] = Cart; } //先藉由Service取得登入者角色資料 string RoleData = membersService.GetRole(LoginMember.Account); //設定JWT JwtService jwtService = new JwtService(); //從Web.Config撈出資料 //Cookie名稱 string cookieName = WebConfigurationManager.AppSettings["CookieName"].ToString(); string Token = jwtService.GenerateToken(LoginMember.Account, RoleData); ////產生一個Cookie HttpCookie cookie = new HttpCookie(cookieName); //設定單值 cookie.Value = Server.UrlEncode(Token); //寫到用戶端 Response.Cookies.Add(cookie); //設定Cookie期限 Response.Cookies[cookieName].Expires = DateTime.Now.AddMinutes(Convert.ToInt32(WebConfigurationManager.AppSettings["ExpireMinutes"])); //重新導向頁面 return(RedirectToAction("Index", "Item")); } else { //有驗證錯誤訊息,加入頁面模型中 ModelState.AddModelError("", ValidateStr); //將資料回填至View中 return(View(LoginMember)); } }