public JsonResult Login(Logon logon) { string status = "The username or password provided is incorrect."; // Verify the fields. if (ModelState.IsValid) { // Authenticate the user. if (UserManager.ValidateUser(logon, Response)) { // Redirect to the secure area. if (string.IsNullOrWhiteSpace(logon.RedirectUrl)) { logon.RedirectUrl = "/"; } status = "OK"; } } return Json(new { RedirectUrl = logon.RedirectUrl, Status = status }); }
/// <summary> /// Authenticates a user via the MembershipProvider and creates the associated forms authentication ticket. /// </summary> /// <param name="logon">Logon</param> /// <param name="response">HttpResponseBase</param> /// <returns>bool</returns> public static bool ValidateUser(Logon logon, HttpResponseBase response) { bool result = false; if (Membership.ValidateUser(logon.Username, logon.Password)) { // Create the authentication ticket with custom user data. var serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(UserManager.User); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, logon.Username, DateTime.Now, DateTime.Now.AddDays(30), true, userData, FormsAuthentication.FormsCookiePath); // Encrypt the ticket. string encTicket = FormsAuthentication.Encrypt(ticket); // Create the cookie. response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); result = true; } return result; }