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("UsersContext", "Users", "UserId", "UserName", autoCreateTables: true); SimpleRoleProvider roles = (SimpleRoleProvider)Roles.Provider; SimpleMembershipProvider membership = (SimpleMembershipProvider)Membership.Provider; if (!roles.RoleExists("Admin")) { roles.CreateRole("Admin"); } if (!roles.RoleExists("User")) { roles.CreateRole("User"); } if (membership.GetUser("HotFury", false) == null) { WebSecurity.CreateUserAndAccount("HotFury", "000000", new { Email = "mail@[email protected]" }); roles.AddUsersToRoles(new []{"HotFury"}, new[] {"Admin"}); } } 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 MyBlogRepository() { db = new UsersContext(); publications = new PublicationContext(); }
public void EditUser(Users user) { db = new UsersContext(); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); }
public void EditFeedback(Feedbacks updatedFeedback) { updatedFeedback.DateTime = DateTime.Today.ToShortDateString() + " в " + DateTime.Now.ToLongTimeString(); db = new UsersContext();// without this updating dosen't work db.Entry(updatedFeedback).State = EntityState.Modified; db.SaveChanges(); }
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()) { Users user = db.Users.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.Users.Add(new Users { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "Пользователь с таким ником уже существует."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }