public JsonResult JsonLogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); return Json(new { success = true, redirect = returnUrl }); } ModelState.AddModelError("", "The user name or password provided is incorrect."); } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); }
public static void SetLogin(LogOnModel model, ReadModel.UserIndexItem user) { HttpContext.Current.Session.Add("UserID", user.Id); var ticket = new FormsAuthenticationTicket( 1, user.Username, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(60), model.RememberMe, user.SerializeRoles(), FormsAuthentication.FormsCookiePath); string hash = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, hash); if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; HttpContext.Current.Response.Cookies.Add(cookie); }
public JsonResult JsonLogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (Authentication.UserValidated(model)) { var readModel = new ReadModelService.SimpleTwitterReadModelServiceClient(); var user = readModel.GetUsers().Where(n => n.Username == model.UserName).SingleOrDefault(); Authentication.SetLogin(model, user); return Json(new { success = true, redirect = returnUrl }); } ModelState.AddModelError("", "The user name or password provided is incorrect."); } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); }
public static bool UserValidated(LogOnModel model) { var service = new Commanding.SimpleTwitterCommandServiceClient(); var readModel = new ReadModelService.SimpleTwitterReadModelServiceClient(); var user = readModel.GetUsers().Where(n => n.Username == model.UserName).SingleOrDefault(); if (user == null) { return false; } service.ValidateUser(new ValidateUserCommand() { UserID = user.Id, Username = model.UserName, Password = model.Password }); bool validated = readModel.UserValidated(user.Id); return validated; }
public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); return Url.IsLocalUrl(returnUrl) ? (ActionResult) Redirect(returnUrl) : RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "The user name or password provided is incorrect."); } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (Authentication.UserValidated(model)) { var readModel = new ReadModelService.SimpleTwitterReadModelServiceClient(); var user = readModel.GetUsers().Where(n => n.Username == model.UserName).SingleOrDefault(); Authentication.SetLogin(model, user); return Url.IsLocalUrl(returnUrl) ? (ActionResult) Redirect(returnUrl) : RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "The user name or password provided is incorrect."); } // If we got this far, something failed, redisplay form return View(model); }