public ActionResult Create([Bind(Include = "UserId,UserName,Password,FirstName,LastName,EmailId")] User user) { if (ModelState.IsValid) { user.UserId = Guid.NewGuid(); db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
//security check.... prevents cross-site request forgeries....stack overflow... description public ActionResult CreateSpeedDate(SpeedDates input) { if (ModelState.IsValid) { using (var db = new FurryEntities()) { var speedDates = db.SpeedDates.FirstOrDefault(x => x.SpeedDateId == input.SpeedDateId); var newSpeedDates = new SpeedDate(); if (speedDates == null) { newSpeedDates.SpeedDateId = Guid.NewGuid(); newSpeedDates.PostTime = DateTime.Now; if (input != null) { var cookievalue = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value); newSpeedDates.UserId = new Guid(cookievalue.UserData); newSpeedDates.Title = input.Title; newSpeedDates.Description = input.Description; newSpeedDates.City = input.City; newSpeedDates.State = input.State; if (input.JsonAttributes != null) { newSpeedDates.JsonAttributes = input.JsonAttributes; } } //nullable properties that may or may not have a value db.SpeedDates.Add(newSpeedDates); //adds complete 'newSpeedDate' to the table db.SaveChanges(); //saves information in db return(RedirectToAction("MainPage", "Home")); //sends them straight to 'home index' } } //closing using statement destroys db connection //if model state is invalid, returns to register and passes existing 'input' back } return(View(input)); }
public ActionResult Register(RegisterViewModel input) //'input' below is information gathered and entered in to the 'RegisterViewModel' once submitted { if (ModelState.IsValid) { using (var db = new FurryEntities()) { var user = db.Users.FirstOrDefault(x => x.UserName == input.UserName); if (user == null && input.Password == input.RepeatPassword)//put password check in javascript.. //if user already exists, sends then this codes does not run. { var newUser = new User(); //creating a new user that goes in to the db var newProfile = new Profile(); //creating a profile from the info below that goes in to the db newUser.UserId = Guid.NewGuid(); newUser.UserName = input.UserName; newUser.Password = input.Password; newUser.EmailId = input.UserName;//EmailId is the UserName newUser.FirstName = input.FirstName; //above is an entity model of 'User' newProfile.AboutMe = input.AboutMe; newProfile.Birthdate = new DateTime(input.Year, input.Month, input.Day); newProfile.City = input.City; newProfile.Country = input.Country; newProfile.DisplayName = input.DisplayName; newProfile.Gender = input.Gender.ToString(); newProfile.GenderSeeking = input.GenderSeeking.ToString(); newProfile.ProfileId = newUser.UserId; //above is an entity model of 'Profile' if (input.JsonAttributes != null) { newProfile.JsonAttributes = input.JsonAttributes; } if (input.Avatar != null) { newProfile.Avatar = input.Avatar; } if (input.LastName != null) { newUser.LastName = input.LastName; } //nullable properties that may or may not have a value db.Users.Add(newUser); //adds complete 'newUser' to the Users table db.Profiles.Add(newProfile); //does the same for Profiles table db.SaveChanges(); //saves information in db var persistentCookie = input.RememberMe; var ticket = new FormsAuthenticationTicket( 1, input.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), persistentCookie, newProfile.ProfileId.ToString() ); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)) { Path = FormsAuthentication.FormsCookiePath }; if (persistentCookie) { cookie.Expires = ticket.Expiration; } Response.Cookies.Add(cookie); //FormsAuthentication.SetAuthCookie(input.UserName, input.RememberMe);//adds authentication cookie return(RedirectToAction("Index", "Home"));//sends them straight to 'home index' } }//closing using statement destroys db connection } return(View(input));//if model state is invalid, returns to register and passes existing 'input' back }