A
A
Alexey Semiglazov2020-11-29 20:44:21
ASP.NET
Alexey Semiglazov, 2020-11-29 20:44:21

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>

2. Also added a connection string for EntityFramework in the web.config file:
<add name="JobSeekerRegistrationContext" connectionString="Data Source= 
                         (LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\FormsAuthJobSeeker.mdf'; Initial 
                          Catalog=FormsAuthJobSeeker;Integrated Security=True"

3. Created the "Applicant" model:
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; }
                   }


4. I create a data context for interacting with the database:
public class JobSeekerContext: DbContext
                       {
                       public JobSeekerContext():base("JobSeekerRegistrationContext") { }
                       public DbSet<JobSeeker> JobSeekers { get; set; }
                        }

5. I create a model for applicant registration:
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; }
               }

6.Created a form for registration of the "Applicant". The form is a modal window in
the Index view, which is the home page of my application. When you click on the
Register button, this modal window will be displayed:
<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>

7. Created a JobSeekerAccount controller with the Register method, which should receive data
from the POST request, which is presented above and save the information in the database:
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);


8. The assembly is assembled without errors. After entering the data and submitting the form, the following error is displayed:

Server error in application '/'.
Could not find this resource.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) may have been removed, renamed, or temporarily unavailable. Review the following URL and verify that it is entered correctly.

Requested URL: /JobSeekerAccount/Register

Version Information: Microsoft .NET Framework, version:4.0.30319; ASP.NET, version: 4.8.4261.0 I WOULD

BE GRATEFUL FOR ANY HELP!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-12-01
@firedragon

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

V
Vladislav, 2020-11-29
@Jewish_Cat

Startup.cs show
most likely did not miss the stage with routing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question