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:
- public ActionResult Inregistrare()
- {
- return View("Inregistrare", new Useri());
- }
- [HttpPost]
- public ActionResult Inregistrare(Useri model)
- { using ( var context = new Entities())
- {
- if (ModelState.IsValid)
- {
- if (context.Useri.Any(x => x.Email == model.Email))
- { ViewBag.Notification = "Exista deja un cont cu aceasta adresa de email.";
- return View();
- }
- else
- {
- context.Useri.Add(model);
- context.SaveChanges();
- return RedirectToAction("Autentificare");
- }
- }
- }
- return View();
The Log-in part:
- public ActionResult Autentificare()
- {
- return View("Autentificare", new Utilizatori());
- }
- [HttpPost]
- public ActionResult Autentificare(Utilizatori model)
- {
- using (var context = new Entities())
- {
- bool Validare = context.Useri.Any(a => a.Email == model.Email && a.Parola == model.Parola);
- if (Validare)
- {
- FormsAuthentication.SetAuthCookie(model.Email, false);
- //Session["Cont"] = ;
- return RedirectToAction("Index", "Licitaties");
- }
- ModelState.AddModelError("", "Date incorecte");
- return View();
- }
- }
- public ActionResult Delogare()
- {
- FormsAuthentication.SignOut();
- return RedirectToAction("Autentificare");
- }
So there in "Validare" I verify if the informations from database = with the informations from the view
Here is the code for "Utilizatori" :
- public class Utilizatori
- { public int Id { get; set; }
- public string Email { get; set; }
- public string Parola { get; set; }
And here is the code for Users ( the table from my db) :
- public partial class Useri
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
- public Useri()
- {
- this.Oferte = new HashSet
(); - this.UseriRol = new HashSet
(); - }
- public int IdUser { get; set; }
- [Required(ErrorMessage = "Tastati un nume")]
- public string Nume { get; set; }
- [Required(ErrorMessage = "Tastati o adresa de mail")]
- [EmailAddress(ErrorMessage = "Scrieti o adresa de mail valida.")]
- public string Email { get; set; }
- [Required]
- [RegularExpression(@"^(?
\()?0(?:(?:72|74|75|76|77|78)(?(paren)\))(? )]\d)(?!\k {6})\d{6}|(?:251|351)(?(paren)\))(? \d)(?!\k {5})\d{5})$" - public string Telefon { get; set; }
- [Required(ErrorMessage = "Tastati o parola")]
- public string Parola { get; set; }
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection
Oferte { get; set; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection
UseriRol { get; set; } - }
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...
Sachin SinghPosted Apr 27, 2021, 3:58 PM
Guest UserPosted Apr 27, 2021, 6:16 PM
Sachin SinghPosted Apr 27, 2021, 6:07 PM
Guest UserPosted Apr 27, 2021, 5:40 PM