public ActionResult Create([Bind(Include = "UserName,UserPhone,UserEmail,UserPassword,UserPasswordConfirm,UserFullName,IAmOwner")] User user) { ModelState.Remove("UserGroupId"); ModelState.Remove("UserPasswordSalt"); if (ModelState.IsValid) { var crypto = new SimpleCrypto.PBKDF2(); User suser = new User(); User vusr = db.Users.Where(u=>u.UserName == user.UserName).FirstOrDefault(); if(vusr != null) { ModelState.AddModelError("UserName", "Acest nume de utilizator exista deja. Va rog alegeti altul!"); return View("Register", user); } suser.UserName = user.UserName; if (user.UserPhone != null) if (IsPhoneNumber(user.UserPhone)) { suser.UserPhone = user.UserPhone; } else { ModelState.AddModelError("UserPhone", "Campul Numar de telefon trebuie sa respecte formatul unui numar de telefon!"); return View("Register", user); } User vemail = db.Users.Where(u => u.UserEmail == user.UserEmail).FirstOrDefault(); if (vemail != null) { ModelState.AddModelError("UserEmail", "Aceasta adresa de email este deja utilizata. Va rog alegeti alta!"); return View("Register", user); } suser.UserEmail = user.UserEmail; suser.UserFullName = user.UserFullName; if(user.UserPassword.Length>20) { ModelState.AddModelError("UserPassword", "Campul Parola trebuie sa aiba maxim 20 de caractere!"); return View("Register", user); } suser.UserPassword = crypto.Compute(user.UserPassword); suser.UserPasswordConfirm = suser.UserPassword; suser.UserPasswordSalt = crypto.Salt; string owner = user.IAmOwner ? "Proprietar" : "Jucator"; suser.UserGroupID = db.UserGroups.Where(e => e.UserGroupName == owner).FirstOrDefault().IDUserGroup; suser.UserGroup = db.UserGroups.Where(e => e.IDUserGroup == suser.UserGroupID).FirstOrDefault(); try { db.Users.Add(suser); db.SaveChanges(); FormsAuthentication.SetAuthCookie(suser.UserName, false); } catch(System.Data.Entity.Validation.DbEntityValidationException er) { foreach (var validationErrors in er.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { ModelState.AddModelError(validationError.PropertyName, validationError.ErrorMessage); } } return View("Edit", user); } return RedirectToAction("Index", "Home"); } ViewBag.UserGroupID = new SelectList(db.UserGroups, "IDUserGroup", "UserGroupName", user.UserGroupID); return View("Register",user); }
public ActionResult Login(User usr) { if (usr.UserName == null || usr.UserPassword == null) { ModelState.AddModelError("", "Utilizator si parola sunt obligatorii!"); } else { byte val = ValidateLogin(usr.UserName, usr.UserPassword); switch(val) { case 0: ModelState.AddModelError("", "Utilizator sau parola sunt gresite!"); break; case 1: ModelState.AddModelError("", "Utilizator este inactiv! Va rugam contactati administratorul in pagina Contacteaza-ne."); break; case 2: ModelState.AddModelError("", "Utilizator nu exista in baza de date dar puteti sa il creati!"); break; case 3: FormsAuthentication.SetAuthCookie(usr.UserName, false); return RedirectToAction("Index", "Home"); } } return View(usr); }