Guest User

Guest User

  • Tech Writer
  • 64
  • 2.8k

Get value for session from Forms Authentication

Apr 27 2021 3:48 PM
Hello, I have a website to make for a school project. The thing is that I made the authentication with Forms Authentication and for the log-in part I created a new class "Utilizatori" that is not in the database. I just want to get in a Session["test"] the IdUser.
 
The registration part:
  1. public ActionResult Inregistrare()  
  2. {  
  3. return View("Inregistrare"new Useri());  
  4. }  
  5. [HttpPost]  
  6. public ActionResult Inregistrare(Useri model)  
  7. using ( var context = new Entities())  
  8. {  
  9. if (ModelState.IsValid)  
  10. {  
  11. if (context.Useri.Any(x => x.Email == model.Email))  
  12. { ViewBag.Notification = "Exista deja un cont cu aceasta adresa de email.";  
  13. return View();  
  14. }  
  15. else  
  16. {  
  17. context.Useri.Add(model);  
  18. context.SaveChanges();  
  19. return RedirectToAction("Autentificare");  
  20. }  
  21. }  
  22. }  
  23. return View();  
The Log-in part:
  1. public ActionResult Autentificare()  
  2. {  
  3. return View("Autentificare"new Utilizatori());  
  4. }  
  5. [HttpPost]  
  6. public ActionResult Autentificare(Utilizatori model)  
  7. {  
  8. using (var context = new Entities())  
  9. {  
  10. bool Validare = context.Useri.Any(a => a.Email == model.Email && a.Parola == model.Parola);  
  11. if (Validare)  
  12. {  
  13. FormsAuthentication.SetAuthCookie(model.Email, false);  
  14. //Session["Cont"] = ;  
  15. return RedirectToAction("Index""Licitaties");  
  16. }  
  17. ModelState.AddModelError("""Date incorecte");  
  18. return View();  
  19. }  
  20. }  
  21. public ActionResult Delogare()  
  22. {  
  23. FormsAuthentication.SignOut();  
  24. return RedirectToAction("Autentificare");  
  25. }  
So there in "Validare" I verify if the informations from database = with the informations from the view
 
Here is the code for "Utilizatori" :
  1. public class Utilizatori  
  2. public int Id { getset; }  
  3. public string Email { getset; }  
  4. public string Parola { getset; }  
And here is the code for Users ( the table from my db) :
  1. public partial class Useri  
  2. {  
  3. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  4. public Useri()  
  5. {  
  6. this.Oferte = new HashSet<Oferte>();  
  7. this.UseriRol = new HashSet<UseriRol>();  
  8. }  
  9. public int IdUser { getset; }  
  10. [Required(ErrorMessage = "Tastati un nume")]  
  11. public string Nume { getset; }  
  12. [Required(ErrorMessage = "Tastati o adresa de mail")]  
  13. [EmailAddress(ErrorMessage = "Scrieti o adresa de mail valida.")]  
  14. public string Email { getset; }  
  15. [Required]  
  16. [RegularExpression(@"^(?<paren>\()?0(?:(?:72|74|75|76|77|78)(?(paren)\))(?<first>\d)(?!\k<first>{6})\d{6}|(?:251|351)(?(paren)\))(?<first>\d)(?!\k<first>{5})\d{5})$")]  
  17. public string Telefon { getset; }  
  18. [Required(ErrorMessage = "Tastati o parola")]  
  19. public string Parola { getset; }  
  20. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  21. public virtual ICollection<Oferte> Oferte { getset; }  
  22. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  23. public virtual ICollection<UseriRol> UseriRol { getset; }  
  24. }  
The thing is the "Utilizatori" have an Id but remains 0 everytime while in the table from my database I have : IdUser :8 , Name: Alex and so on...

Answers (4)