DEV Community

Gomes
Gomes

Posted on

Problem with Login in MVC

Recently I started making a project for my programming classes.
The project is called DarkBox (you can find more information about it on my GitHub)

During the development of the Login Form, I use a test profile to check if everything is working. When I use the email and password, the login doesn't redirect me to where I intended.

Here's the code from my controller:

//(POST)
[HttpPost]
public async Task<IActionResult> Login(string email, string password)
{
    if (!ModelState.IsValid)
    {
        return View();
    }
    // Gets the user in DB
    var user = await _context.Users
        .FirstOrDefaultAsync(u => u.Email == email);

    if (user == null || user.PasswordHash != password)
    {
        //Error Message
        ModelState.AddModelError(string.Empty, "Email ou senha inválidos.");
        ViewBag.Email = email; // Keep the email in view
        return View();
    }

    // Stores the session
    HttpContext.Session.SetString("UserEmail", user.Email);
    return RedirectToAction("DashboardClient", "DashboardUser");
}
Enter fullscreen mode Exit fullscreen mode

And here's the code for the login form:

<form method="post" action="Login">
    <div class="d-flex flex-column align-items-center">
        <br>
    </div>
    <h1 class="h3 mb-3 fw-normal text-center">Iniciar Sessão</h1>

    <div class="form-floating">
        <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
        <label for="floatingInput">Email</label>
    </div>
    <div class="form-floating">
        <input type="password" class="form-control" id="floatingPassword" placeholder="Password">
        <label for="floatingPassword">Palavra-Passe</label>
    </div>

    <div class="d-flex justify-content-between align-items-center my-3">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" value="remember-me" id="flexCheckDefault">
            <label class="form-check-label" for="flexCheckDefault">
                Lembrar-me
            </label>
        </div>
        <a href="recuperar-senha.html" class="text-primary">Esqueceu a senha?</a>
    </div>

    <button class="btn btn-success w-100 py-2" type="submit">Iniciar Sessão</button>
    <div class="">
        <p class="mt-3 text-center">
            Não tem conta? <a href="register.html" class="text-primary fw-bold">Criar Conta</a>
        </p>
    </div>
</form>
Enter fullscreen mode Exit fullscreen mode

I'd appreciate all kinds of help, thank you!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more