public ActionResult Login(FormCollection fc) { string userName = fc["txtUserName"]; string userPwd = fc["txtUserPwd"]; bool errors = false; if (string.IsNullOrEmpty(userName)) { ModelState.AddModelError("UserName", "用户名不能为空"); errors = true; } if (string.IsNullOrEmpty(userPwd)) { ModelState.AddModelError("UserPwd", "密码不能为空"); errors = true; } if (ModelState.IsValid && !errors) { //判断用户名和密码是否正确 bool isLogin = MemberService.AdminMemberValidator(userName, userPwd); if (isLogin) { //写Cookie DateTime cookieExpires = DateTime.Now.Add(FormsAuthentication.Timeout); FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, cookieExpires, false, "", FormsAuthentication.FormsCookiePath); //加密内容 string HashTicket = FormsAuthentication.Encrypt(Ticket); //添加Cookie HttpCookie lcookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket); lcookie.Expires = cookieExpires; lcookie.Domain = FormsAuthentication.CookieDomain; Response.Cookies.Add(lcookie); Response.Cookies[FormsAuthentication.FormsCookieName].Expires = cookieExpires; return(Redirect("/")); } ModelState.AddModelError("Error", "用户名或密码错误!"); } return(View()); }