Answer the question
In order to leave comments, you need to log in
Problems when implementing Authorization in Forms mode in MVC 5?
Good evening everyone. I'm learning ASPNet MVC5 based on reading Metanit and implementing a small pet project - sort of HH but in a very simplified form.
There was a problem creating the "Applicant" authentication system in Forms mode.
1. Enabled Forms authentication in the web.config file:
<authentication mode="Forms">
<forms name="cookies" timeout="2880" loginUrl="~/Home/Index" />
</authentication>
<add name="JobSeekerRegistrationContext" connectionString="Data Source=
(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\FormsAuthJobSeeker.mdf'; Initial
Catalog=FormsAuthJobSeeker;Integrated Security=True"
public class JobSeeker
{
//Идентификационный номер соискателя
public int JobSeekerID { get; set; }
//Фамилия соискателя
public string LastName { get; set; }
//Имя соискателя
public string FirstName { get; set; }
//Электронная почта
public string Email { get; set; }
//Пароль
public string Password { get; set; }
}
public class JobSeekerContext: DbContext
{
public JobSeekerContext():base("JobSeekerRegistrationContext") { }
public DbSet<JobSeeker> JobSeekers { get; set; }
}
public class JobSeekerRegistration
{
//фамилия соискателя
[Required]
public string LastName { get; set; }
//Имя соискателя
[Required]
public string FirstName{ get; set; }
//Почта
[Required]
public string Email { get; set; }
//Пароль
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
<form id="authtorization_joobseeker_form" action="/JobSeekerAccount/Register" method="post">
<p id="last_name">
<label id="joobseeker_last_name" for="joobseeker_last_name">Фамилия</label>
<input id="joobseeker_last_name" type="text" name="LastName">
</p>
<p id="first_name">
<label id="joobseeker_first_name" for="joobseeker_first_name">Имя</label>
<input id="joobseeker_first_name" type="text" name="FirstName">
</p>
<p id="mail">
<label id="joobseeker_mail" for="joobseeker_mail">Email</label>
<input id="joobseeker_mail" type="mail" name="Email">
</p>
<p id="password">
<label id="joobseeker_password" for="joobseeker_password">Пароль</label>
<input id="joobseeker_password" type="password" name="Password">
</p>
<input id="authtorization_joobseeker_submit" class="submit" type="submit" value="Регистрация">
</form>
public class JobSeekerAccount : Controller
{
// GET: JobSeekerAccount
[HttpPost]
public ActionResult Register(JobSeekerRegistration model)
{
if (ModelState.IsValid)
{
JobSeeker jobSeeker = null;
using (JobSeekerContext db = new JobSeekerContext())
{
jobSeeker = db.JobSeekers.FirstOrDefault(JobSeeker => JobSeeker.LastName == model.LastName);
}
if (jobSeeker==null)
{
using (JobSeekerContext db = new JobSeekerContext())
{
db.JobSeekers.Add(new JobSeeker { LastName = model.LastName, FirstName = model.FirstName, Email = model.Email, Password = model.Password });
db.SaveChanges();
jobSeeker = db.JobSeekers.Where(JobSeeker => JobSeeker.Email == model.Email && JobSeeker.Password == model.Password).FirstOrDefault();
}
if (jobSeeker != null)
{
FormsAuthentication.SetAuthCookie(model.Email, true);
return RedirectToAction("Index", "Home");
}
}
return View(model);
Answer the question
In order to leave comments, you need to log in
https://docs.microsoft.com/ru-ru/aspnet/mvc/overvi...
Most likely it's the controller, it doesn't have the right ending, the article has a description of why this is so
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question