public override void CreateRole(string roleName) { Role newRole = new Role() { Name = roleName }; BelarusAttractionsContext db = new BelarusAttractionsContext(); db.Roles.Add(newRole); db.SaveChanges(); }
public ActionResult Registration(RegisterModel model) { if (ModelState.IsValid) { User user = null; using (BelarusAttractionsContext db = new BelarusAttractionsContext()) { user = unit.Users.GetAll().FirstOrDefault(u => u.Email == model.Email); } if (user == null) { // создаем нового пользователя using (BelarusAttractionsContext db = new BelarusAttractionsContext()) { unit.Users.Create(new User { Email = model.Email, Password = model.Password, FirstName = model.FirstName, LastName = model.LastName, ConfirmPassword = model.ConfirmPassword, Gender = model.Gender, Skype = model.Skype, Telephone = model.Telephone, RoleId = 3 }); unit.Save(); //db.SaveChanges(); user = unit.Users.GetAll().Where(u => u.Email == model.Email && u.Password == model.Password).FirstOrDefault(); } // если пользователь удачно добавлен в бд if (user != null) { FormsAuthentication.SetAuthCookie(model.Email, true); return(RedirectToAction("Index", "Home")); } } else { ModelState.AddModelError("Email", "Пользователь с таким логином уже существует"); } } return(View(model)); }
public override string[] GetRolesForUser(string username) { string[] role = new string[] { }; using (BelarusAttractionsContext db = new BelarusAttractionsContext()) { // Получаем пользователя User user = db.Users.FirstOrDefault(u => u.Email == username); if (user != null) { // получаем роль Role userRole = db.Roles.Find(user.RoleId); if (userRole != null) { role = new string[] { userRole.Name } } ; } } return(role);; }
public override bool IsUserInRole(string username, string roleName) { bool outputResult = false; // Находим пользователя using (BelarusAttractionsContext db = new BelarusAttractionsContext()) { // Получаем пользователя User user = db.Users.FirstOrDefault(u => u.Email == username); if (user != null) { // получаем роль Role userRole = db.Roles.Find(user.RoleId); //сравниваем if (userRole != null && userRole.Name == roleName) { outputResult = true; } } } return(outputResult); }
public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { // поиск пользователя в бд User user = null; using (BelarusAttractionsContext db = new BelarusAttractionsContext()) { user = unit.Users.GetAll().FirstOrDefault(u => u.Email == model.Email && u.Password == model.Password); } if (user != null) { FormsAuthentication.SetAuthCookie(model.Email, true); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("Password", "Пользователя с таким логином и паролем нет"); } } return(View(model)); }
public CityRepository(BelarusAttractionsContext context) { this._applicationDbContext = context; }