void Application_PostAuthenticateRequest(object sender, EventArgs e) { HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie == null) return; string encTicket = authCookie.Value; if (!String.IsNullOrEmpty(encTicket)) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket); WebUser id = new WebUser(ticket); GenericPrincipal prin = new GenericPrincipal(id, null); HttpContext.Current.User = prin; } }
public ActionResult Login(LogInModel model) { ViewBag.PageId = "page5"; if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("", "用户名密码不能为空"); return View(); } Session.RemoveAll(); var authTicket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), true, WebUser.CreateUserData(model.Password)); WebUser id = new WebUser(authTicket); GenericPrincipal prin = new GenericPrincipal(id, null); System.Web.HttpContext.Current.User = prin; string encTicket = FormsAuthentication.Encrypt(authTicket); System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); if (!ManageService.Authentication(model.UserName, Core.Common.Text.Sha256(model.Password))) { FormsAuthentication.SignOut(); ModelState.AddModelError("", "登陆失败!"); return View(); } else { Session["Username"] = model.UserName; return RedirectToAction("Index", "Houtai"); } }