public ActionResult SaveRegisterDetails(Register registerDetails) { //We check if the model state is valid or not. We have used DataAnnotation attributes. //If any form value fails the DataAnnotation validation the model state becomes invalid. if (ModelState.IsValid) { //create database context using Entity framework using (var databaseContext = new BookHubDBEntities()) { //If the model state is valid i.e. the form values passed the validation then we are storing the User's details in DB. User reglog = new User { //Save all details in RegitserUser object Username = registerDetails.Username, Email = registerDetails.Email, Password = registerDetails.Password }; try { //Calling the SaveDetails method which saves the details. databaseContext.Users.Add(reglog); databaseContext.SaveChanges(); IOWork.MkDir(databaseContext.Users.Where(user => user.Email == reglog.Email).FirstOrDefault().UserID.ToString()); } 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; } catch (DbUpdateException) { //ViewBag.Message = "<div role=\"alert\" class=\"alert alert-danger\" style><span><strong>Username or Email already exsist</strong></span></div>"; ViewBag.Falen = "Username or Email already exsist"; return(View("Register")); } } ViewBag.Success = "Register was successful"; //ViewBag.Message = "<div role=\"alert\" class=\"alert alert-success\" style><span><strong>Register was successful</strong></span></div>"; return(View("Register")); } else { //If the validation fails, we are returning the model object with errors to the view, which will display the error messages. return(View("Register", registerDetails)); } }
//function to check if User is valid or not public User IsValidUser(LoginViewModel model) { using (var dataContext = new BookHubDBEntities()) { //Retireving the user details from DB based on username and password enetered by user. User user = dataContext.Users.Where(query => query.Email.Equals(model.Email) && query.Password.Equals(model.Password)).SingleOrDefault(); //If user is present, then true is returned. if (user == null) { return(null); } //If user is not present false is returned. else { return(user); } } }
public string GetIconUrl() { using (var dataContext = new BookHubDBEntities()) { User user = dataContext.Users.Where(query => query.Email.Equals(User.Identity.Name)).SingleOrDefault(); if (user == null) { return(null); } //32x32 if (user.Icon == true) { return($"/Content/img/media/{user.UserID}/conversions/avatar-thumb.jpg"); } else { return($"/Content/img/main/avatar-thumb.png"); } } }