public SimpleMembershipInitializer() { Database.SetInitializer<ASPHotel.Models.HotelEntities>(null); try { using (var context = new ASPHotel.Models.HotelEntities()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } //IMPORTANT! Keep the line below as a comment! //WebSecurity.InitializeDatabaseConnection("HotelContext", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
private bool IsValid(string username, string password) { bool IsValid = false; using (var db = new ASPHotel.Models.HotelEntities()) { var user = db.Users.FirstOrDefault(u => u.Username == username); if (user != null) { if (user.Password == password) { IsValid = true; } } } return IsValid; }
public ActionResult Register(ASPHotel.Models.User user) { if (ModelState.IsValid) { using(var db = new ASPHotel.Models.HotelEntities()){ //Checks if the username doesn't already exist if (db.Users.Find(user.Username) != null && user.Username.Equals(db.Users.Find(user.Username).Username)) { ViewData["error"] = "Please choose a different username."; return View(user); } //If the username is unique, registers the new user else { var newUser = db.Users.Create(); newUser.Username = user.Username; newUser.Password = user.Password; newUser.Role_Name = "guest"; newUser.IsAuthorised = 0; db.Users.Add(newUser); db.SaveChanges(); return RedirectToAction("Login", "Account"); } } } // If we got this far, something failed, redisplay form return View(user); }