public SimpleMembershipInitializer() { try { using (var context = new UsersContext()) { if (!context.DatabaseExists()) { context.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("AccountConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); if (!Roles.RoleExists(JournalStatic.AdminRole)) Roles.CreateRole(JournalStatic.AdminRole); if (!Roles.RoleExists(JournalStatic.SchoolAdminRole)) Roles.CreateRole(JournalStatic.SchoolAdminRole); if (!Roles.RoleExists(JournalStatic.Teacher)) Roles.CreateRole(JournalStatic.Teacher); if (!Roles.RoleExists(JournalStatic.Parent)) Roles.CreateRole(JournalStatic.Parent); if (!Roles.RoleExists(JournalStatic.Pupil)) Roles.CreateRole(JournalStatic.Pupil); } 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); } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { using (var db = new UsersContext()) { if (!WebSecurity.UserExists(model.UserName)) { var userProfile = new UserProfile(); userProfile.SchoolId = model.SchoolId; userProfile.UserName = model.UserName; db.UserProfiles.InsertOnSubmit(userProfile); db.SubmitChanges(ConflictMode.ContinueOnConflict); } } WebSecurity.CreateAccount(model.UserName, model.Password); WebSecurity.Login(model.UserName, model.Password); //return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } using (var db = new UsersContext()) { if (!db.DatabaseExists()) db.CreateDatabase(); ViewBag.Schools = db.Schools.ToList(); } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Register() { using (var db = new UsersContext()) { if(!db.DatabaseExists()) db.CreateDatabase(); ViewBag.Schools = db.Schools.ToList(); } return View(); }