public ActionResult Register(RegisterModel model) { string userNameIdentifier = GetCurrentUserNameIdentifier(); if (ModelState.IsValid) { try { using (var db = new UsersContext()) { if (DefaultConfigurationStore.Current.ValidateInvitationCodes || string.Compare(model.InvitationCode, model.EMail, true) != 0) { var invitationCodes = from i in db.InvitationCodes where i.Code == model.InvitationCode && i.UsedBy == null select i; if (invitationCodes.Count() == 0) throw new Exception("the code does not exist!"); invitationCodes.First().UsedBy = userNameIdentifier; } db.UserProfiles.Add(new UserProfile { EMail = model.EMail, UserName = model.Name, NameIdentifier = userNameIdentifier }); db.SaveChanges(); } return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } catch (Exception e) { ModelState.AddModelError("", e.Message); } } return View(model); }
public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) if (!context.Database.Exists()) ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); WebSecurity.InitializeDatabaseConnection("DefaultConnection", "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); } }
//public static bool IsRegistered() public bool IsRegistered() { string userNameIdentifier = GetCurrentUserNameIdentifier(); if (!string.IsNullOrEmpty(userNameIdentifier)) { using (var db = new UsersContext()) { var userProfiles = from i in db.UserProfiles where i.NameIdentifier == userNameIdentifier select i; if (userProfiles.Count() > 0) { ViewBag.Name = userProfiles.First().UserName; ViewBag.EMail = userProfiles.First().EMail; return true; } } } return false; }