public ActionResult Connexion(PersonneVM personne, string returnUrl) { string message = ""; Personne user = new Personne(); user.Email = personne.Email; user.Password = personne.Password; using (Context context = new Context()) { var v = context.Personnes.Where(a => a.Email == user.Email && a.IsEmailVerified == true).FirstOrDefault(); if (v != null) { if (string.Compare(Crypto.Hash(personne.Password), v.Password) == 0) { //TODO ajouter le booléen RememberMe aux entities et à la base de données //int timeout = personne.RememberMe ? 525600 : 120; int timeout = 120; var ticket = new FormsAuthenticationTicket(v.Id.ToString(), false, timeout); string encrypted = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted); cookie.Expires = DateTime.Now.AddMinutes(timeout); cookie.HttpOnly = true; Response.Cookies.Add(cookie); return(RedirectToAction("Index", "Home")); } else { message = "Mot de passe Invalide"; } } else { message = "Email Invalide"; } } ViewBag.Message = message; return(View()); }
[ValidateAntiForgeryToken] // permet de lutter contre la contrefaçon d'une requete public ActionResult Enregistrement([Bind(Exclude = "IsEmailVerified, ActivationCode")] PersonneVM personne) { bool status = false; string message = ""; Bitmap bmp; Graphics g; if (ModelState.IsValid) //Despite its name, it doesn't actually know anything about any model classes. //The ModelState represents a Enumerable of name and value pairs that were submitted to the server during a POST. //It also contains a Enumerable of error messages for each value submitted { //Email already exists var emailExists = EmailExists(personne.Email); if (emailExists) { //ModelState.AddModelError("EmailExists", "Email already exists"); ViewBag.Message = "Cet email existe deja"; ViewBag.Status = false; return(View(personne)); } //PHOTO string fileName = ""; if (personne.FichierPhoto != null) { if (!String.IsNullOrEmpty(personne.FichierPhoto.FileName)) { fileName = Path.GetFileNameWithoutExtension(personne.FichierPhoto.FileName); string extension = Path.GetExtension(personne.FichierPhoto.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; personne.Photo = fileName; fileName = Path.Combine(Server.MapPath("~/Images/"), fileName); personne.FichierPhoto.SaveAs(fileName); Image myImage = Image.FromFile(fileName); if (extension != ".jpg" && extension != ".bmp") { ViewBag.Message = "Le fichier image doit avoir une extension .jpg ou .bmp"; ViewBag.Status = false; if (System.IO.File.Exists(fileName)) { myImage.Dispose(); System.IO.File.Delete(fileName); } return(View(personne)); } //On recadre l'image if (personne.CropW > 0 && personne.CropH > 0) { bmp = new Bitmap(personne.CropW, personne.CropH, myImage.PixelFormat); g = Graphics.FromImage(bmp); g.DrawImage(myImage, new Rectangle(0, 0, personne.CropW, personne.CropH), new Rectangle(personne.CropX, personne.CropY, personne.CropW, personne.CropH), GraphicsUnit.Pixel); } else { bmp = new Bitmap(myImage.Width, myImage.Height, myImage.PixelFormat); g = Graphics.FromImage(bmp); g.DrawImage(myImage, new Rectangle(0, 0, myImage.Width, myImage.Height), new Rectangle(0, 0, myImage.Width, myImage.Height), GraphicsUnit.Pixel); } if (bmp.Width > 300) // si largeur de l'image supérieure à 300px on la redimmensionne { float ratio = (float)(bmp.Height) / (float)(bmp.Width); int newHeight = (int)(300 * ratio); bmp = ResizeImage(bmp, 300, newHeight); } System.Drawing.Imaging.ImageFormat frm = myImage.RawFormat; myImage.Dispose(); bmp.Save(fileName, frm); } } //Generate activation code personne.ActivationCode = Guid.NewGuid(); //Password hashing personne.Password = Crypto.Hash(personne.Password); personne.ConfirmPassword = Crypto.Hash(personne.ConfirmPassword); personne.IsEmailVerified = false; //save date to database Personne user = new Personne(); user.Prenom = personne.Prenom; user.Nom = personne.Nom; user.Photo = personne.Photo; user.DateDeNaissance = personne.DateDeNaissance; user.Email = personne.Email; user.NumeroTel = personne.NumeroTel; user.Permis = personne.Permis; user.Adresse = personne.Adresse; user.CodePostal = personne.CodePostal; user.Commune = personne.Commune; user.Password = personne.Password; user.IsEmailVerified = personne.IsEmailVerified; user.ActivationCode = personne.ActivationCode; using (Context context = new Context()) { context.Personnes.Add(user); context.SaveChanges(); //Send email to user //TODO : faire le insert une fois certain que l'email a été envoyé SendVerificationLinkEmail(user.Email, user.ActivationCode.ToString()); message = "L'enregistrement s'est déroulé avec succès, toutefois il vous faut cliquer sur ce lien qui vous a été envoyé à " + user.Email + " pour valider votre compte."; status = true; } } else { message = "invalid request"; } ViewBag.Message = message; ViewBag.Status = status; return(View(personne)); }
public ActionResult Deconnexion(PersonneVM personne) { FormsAuthentication.SignOut(); return(RedirectToAction("Connexion", "User")); }