TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
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<Oferte>();
this
.UseriRol =
new
HashSet<UseriRol>();
}
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(@
"^(?<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})$"
)]
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> Oferte {
get
;
set
; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Usage"
,
"CA2227:CollectionPropertiesShouldBeReadOnly"
)]
public
virtual
ICollection<UseriRol> 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...
Reply
Answers (
4
)
Soft Delete Record , Display by selecting Active /Inactive by dropdown
Best and Latest approach to manage Roles and permission for user in