protected override void Seed(UWCContext db) { db.Roles.Add(new Role { Id = UserRoles.ADMIN_ROLE_ID, Name = UserRoles.ADMIN_ROLE_NAME }); db.Roles.Add(new Role { Id = UserRoles.TEACHER_ROLE_ID, Name = UserRoles.TEACHER_ROLE_NAME }); db.Roles.Add(new Role { Id = UserRoles.STUDENT_ROLE_ID, Name = UserRoles.STUDENT_ROLE_NAME }); Guid uid = Guid.NewGuid(); db.Users.Add(new User { Id = uid, Surname = "Иванов", Name = "Иван", Patronymic = "Иванович", Age = 22, Email = "*****@*****.**", Password = Rfc2898Encoder.Encode("PK2a82mxu", uid.ToString()), RoleId = UserRoles.ADMIN_ROLE_ID }); base.Seed(db); }
public ActionResult SetAdminRecord(SetAdminRecordModel model) { if (ModelState.IsValid) { using (UWCContext db = new UWCContext()) { string roleName = model.RoleName == "Преподаватель" ? UserRoles.TEACHER_ROLE_NAME : UserRoles.STUDENT_ROLE_NAME; AdminRecord record = db.AdminRecords.FirstOrDefault(r => r.RoleName == roleName); Guid salt = Guid.NewGuid(); if (record != null) { record.Salt = salt; record.Password = Rfc2898Encoder.Encode(model.AccessPassword, salt.ToString()); db.Entry(record).State = EntityState.Modified; db.SaveChanges(); } else { AdminRecord newRecord = new AdminRecord() { Salt = salt, RoleName = roleName, Password = Rfc2898Encoder.Encode(model.AccessPassword, salt.ToString()) }; db.AdminRecords.Add(newRecord); db.SaveChanges(); } return(RedirectToAction("AdminRecordAddedMessage", "Administration")); } } ViewBag.AllowedRoles = new SelectList(new string[] { "Преподаватель", "Студент" }); return(View(model)); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { User user = null; using (UWCContext db = new UWCContext()) { user = db.Users.FirstOrDefault(u => u.Email == model.Email); } if (user == null) { using (UWCContext db = new UWCContext()) { string roleName = model.RoleName == "Преподаватель" ? UserRoles.TEACHER_ROLE_NAME : UserRoles.STUDENT_ROLE_NAME; AdminRecord adminRecord = db.AdminRecords.FirstOrDefault(r => r.RoleName == roleName); if (adminRecord != null) { Guid uid = Guid.NewGuid(); string userSalt = uid.ToString(); string encodedPassword = Rfc2898Encoder.Encode(model.Password, userSalt); if (Rfc2898Encoder.Validate(model.RoleAccessPassword, adminRecord.Password, adminRecord.Salt.ToString())) { User newUser = new User() { Id = uid, Surname = model.Surname, Name = model.Name, Patronymic = model.Patronymic, Age = model.Age, Email = model.Email, Password = encodedPassword, RoleId = model.RoleName == "Преподаватель" ? UserRoles.TEACHER_ROLE_ID : UserRoles.STUDENT_ROLE_ID }; db.Users.Add(newUser); db.SaveChanges(); } else { ModelState.AddModelError("", "Неверный пользовательский или преподавательский пароль"); } user = db.Users.Where(u => u.Email == model.Email && u.Password == encodedPassword).FirstOrDefault(); } else { ModelState.AddModelError("", "Пароль для регистрации с ролью \"" + roleName + "\" еще не задан администратором, попробуйте позже."); } } if (user != null) { FormsAuthentication.SetAuthCookie(model.Email, true); return(RedirectToAction("Index", "Home")); } } else { ModelState.AddModelError("", "Пользоваетль с таким адресом Email уже существует"); } } ViewBag.AllowedRoles = new SelectList(new string[] { "Преподаватель", "Студент" }); return(View(model)); }