public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", false); //ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DefaultConnection"]; } 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 ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (UsersContext db = new UsersContext()) { UserProfile userNameCheck = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); UserProfile emailCheck = db.UserProfiles.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower()); // Check if user already exists if (userNameCheck != null) { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); } if (emailCheck != null) { ModelState.AddModelError("Email", "That email address in use. Please enter a different email."); ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); } // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName, Email = model.Email, Timezone = model.Timezone, PhoneNumber = String.IsNullOrEmpty(model.PhoneNumber) ? null : model.PhoneNumber, SteamHandle = String.IsNullOrEmpty(model.SteamCommunityName) ? null : model.SteamCommunityName, ListInDirectory = model.ListInDirectory, Signature = null, AvatarFileName = null, BanExpireDate = null }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); Roles.AddUserToRole(model.UserName, "User"); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }