public ActionResult LogIn(LoginDTO loginDTO) { try { if (ModelState.IsValid) { User user = SessionManager.AuthenticateUser(loginDTO.Email, loginDTO.Password); using (var db = new Models.HacForoContainer()) { string userJson = new JavaScriptSerializer().Serialize(TableUserMap.MapTo(user)); string encTicket = SessionManager.GetAuthTicket(user.UserName, userJson); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); } return(RedirectToAction("Index", "Home")); } } catch (Exception) { ModelState.AddModelError("", "Login details are wrong."); } return(View()); }
public User MapTo(TableUserDTO dto) { using (var db = new Models.HacForoContainer()) { return(db.UserSet.Find(dto.Id)); } }
public ActionResult Index() { ViewBag.Title = "Home Page"; using (var db = new Models.HacForoContainer()) { var threads = db.ForumThreadSet.ToList(); ViewBag.Threads = threads.ToList() .Select(ft => Mapper.MapTo(ft)) .OrderByDescending(t => t.CreationDate) .ToList(); } return(View()); }
// GET: User/Details/5 public ActionResult Details(int?id) { using (var db = new Models.HacForoContainer()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } UserDTO userDTO = Mapper.MapTo(db.UserSet.Find(id)); if (userDTO == null) { return(HttpNotFound()); } return(View(userDTO)); } }
public bool CheckUserCanPoint(int userId) { using (var db = new Models.HacForoContainer()) { if (Id == 0 || userId == 0) { UserCanPoint = false; } UserCanPoint = userId != User.Id && db.UserThreadPointsSet.Where(utp => utp.UserId == userId && utp.ThreadId == Id).Count() == 0; } return(UserCanPoint); }
public User MapTo(RegistrationDTO dto) { using (var db = new Models.HacForoContainer()) { var dbModel = db.UserSet.Create(); dbModel.SetPassword(dto.Password); dbModel.Email = dto.Email; dbModel.FirstName = dto.FirstName; dbModel.LastName = dto.LastName; dbModel.UserName = dto.UserName; dbModel.CreationDate = DateTime.Now; dbModel.ProfilePictureLink = dto.ProfilePictureLink; return(dbModel); } }
public ActionResult Edit(RegistrationDTO registrationDTO) { try { if (!ValidateUser(registrationDTO.Id)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var db = new Models.HacForoContainer()) { if (registrationDTO.IsValid(db, ModelState)) { var userDb = db.UserSet.Find(registrationDTO.Id); if (userDb == null) { return(HttpNotFound()); } registrationDTO.UpdateModel(userDb); db.SaveChanges(); return(View("Details", Mapper.MapTo(userDb))); } else { ModelState.AddModelError("", "The data is not valid."); } } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(View()); }
internal static User AuthenticateUser(string email, string password) { var crypto = new SimpleCrypto.PBKDF2(); using (var db = new Models.HacForoContainer()) { User user = db.UserSet.FirstOrDefault(u => u.Email == email); if (user != null) { if (user.Password == crypto.Compute(password, user.PasswordSalt)) { return(user); } } } throw new UnauthorizedAccessException("The login data is invalid"); }
public ActionResult ChangePassword(ChangePasswordDTO newChangePassword) { try { if (ModelState.IsValid) { using (var db = new Models.HacForoContainer()) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; UserDTO cookieUser = SessionManager.GetLoggedUser(authCookie); var userDb = db.UserSet.Find(cookieUser.Id); if (userDb == null) { return(HttpNotFound()); } userDb.SetPassword(newChangePassword.Password); db.SaveChanges(); return(View("Details", Mapper.MapTo(userDb))); } } else { ModelState.AddModelError("", "The data is not correct"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(View()); }
public ForumThread MapTo(TableThreadDTO dto) { ForumThread modelDb; if (dto.Id != 0) { using (var db = new Models.HacForoContainer()) { modelDb = db.ForumThreadSet.Find(dto.Id); } } else { modelDb = new ForumThread(); modelDb.Title = dto.Title; modelDb.UserId = dto.User.Id; modelDb.CreationDate = DateTime.Now; modelDb.ImageLink = dto.ImageLink; } return(modelDb); }
public ActionResult Register(RegistrationDTO registrationDTO) { try { if (ModelState.IsValid) { using (var db = new Models.HacForoContainer()) { if (registrationDTO.IsValid(db, ModelState)) { db.UserSet.Add(RegistrationMap.MapTo(registrationDTO)); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } } } else { ModelState.AddModelError("", "The data is not correct"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(View()); }