public int AjouterEtudiant(string nom, string prenom, string password) { Eleve eleve = new Eleve() { Nom = nom, Prenom = prenom, Password = Crypto.HashPassword(password) }; sctx.Eleves.Add(eleve); sctx.SaveChanges(); return eleve.Id; }
public Eleve Authentifier(string nom, string password) { var eleveConnexion = sctx.Eleves.Where(r => r.Nom == nom).SingleOrDefault(); Eleve dbEleve = new Eleve() { Id = eleveConnexion.Id, Nom = eleveConnexion.Nom, Prenom = eleveConnexion.Prenom, Password = eleveConnexion.Password, Votes = eleveConnexion.Votes, Role = eleveConnexion.Role }; return dbEleve; }
public ActionResult CreerCompte(Eleve poEleve) { using (var dal = new Dal()) { if (dal.EleveExist(poEleve.Nom)) ModelState.AddModelError(String.Empty, "L'éléve éxiste déjà"); else if (ModelState.IsValid) { dal.AjouterEtudiant(poEleve.Nom, poEleve.Prenom, poEleve.Password); return RedirectToAction("login"); } } return RedirectToAction("index", "restaurant"); }
public ActionResult Login(Eleve poEleve) { using (var dal = new Dal()) { Eleve dbEleve = dal.Authentifier(poEleve.Nom, poEleve.Password); if( dbEleve != null) { IdentitySignin(dbEleve); return RedirectToAction("index", "restaurant"); } else { ModelState.AddModelError(String.Empty, "Mot de passe ou identifiant invalide"); return View(); } } }
private void IdentitySignin(Eleve eleve) { var claims = new List<Claim>(); if (eleve.Role != null) claims.Add(new Claim(ClaimTypes.Role, eleve.Role)); // create required claims claims.Add(new Claim(ClaimTypes.NameIdentifier, eleve.Id.ToString())); claims.Add(new Claim(ClaimTypes.Name, eleve.Nom)); var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { AllowRefresh = true, IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddDays(7) }, identity); }