[Authorize] public ActionResult SecureAction() { return View(); }
var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData, "/"); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authenticationCookie);
public ActionResult Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = _userService.Authenticate(model.UserName, model.Password); if (user != null) { var userData = GetUserSpecificData(user); var ticket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData, FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authenticationCookie); if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "Invalid username or password."); } } return View(model); }
如果您的问题仍然存在,请确保在代码中使用Debug和Trace以查找更多信息。