Building a Login And Registration System in ASP.NET MVC

Introduction

Every real-world application needs:

  • User Registration

  • Login System

  • Session Management

  • Logout

Companies always ask this in interviews.

Students search this topic daily.

So this blog has high view potential.

What We Will Build

We will create:

  • User Registration

  • Login Page

  • Session Handling

  • Logout

  • Database Connection using ADO.NET

This is beginner-friendly and easy to understand.

Step 1: Create Database

Open SQL Server and run:

CREATE DATABASE LoginMVCDB;

USE LoginMVCDB;

CREATE TABLE Users
(
    Id INT PRIMARY KEY IDENTITY(1,1),
    Username NVARCHAR(100),
    Email NVARCHAR(100),
    Password NVARCHAR(100)
);

Explanation

  • IDENTITY(1,1) → Auto-increment ID

  • Username → Stores username

  • Email → Stores email

  • Password → Stores password

Note: For learning purposes only. In real projects, always hash passwords.

Step 2: Create ASP.NET MVC Project

  • Open Visual Studio

  • Create New ASP.NET Web Application

  • Choose MVC Template

Step 3: Add Connection String

Open Web.config and add the following:

<connectionStrings>
  <add name="dbcs"
       connectionString="Data Source=YOUR_SERVER;Initial Catalog=LoginMVCDB;Integrated Security=True"
       providerName="System.Data.SqlClient"/>
</connectionStrings>

Replace YOUR_SERVER with your SQL Server name.

Step 4: Create Model

Create User.cs inside the Models folder.

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

Why Model?

Model transfers data between:

  • Controller → View

  • View → Controller

Step 5: Create AccountController

Add namespaces:

using System.Configuration;
using System.Data.SqlClient;
using YourProjectName.Models;

Registration Feature

Registration GET Method

public ActionResult Register()
{
    return View();
}

This shows an empty registration form.

Registration POST Method

[HttpPost]
public ActionResult Register(User user)
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "INSERT INTO Users(Username,Email,Password) VALUES(@Username,@Email,@Password)";
        SqlCommand cmd = new SqlCommand(query, con);

        cmd.Parameters.AddWithValue("@Username", user.Username);
        cmd.Parameters.AddWithValue("@Email", user.Email);
        cmd.Parameters.AddWithValue("@Password", user.Password);

        con.Open();
        cmd.ExecuteNonQuery();
    }

    ViewBag.Message = "Registration Successful!";
    return View();
}

Explanation

  • SqlConnection → Connect to database

  • SqlCommand → Execute SQL query

  • Parameters → Prevent SQL Injection

  • ExecuteNonQuery() → Used for insert operations

Registration View

@model YourProjectName.Models.User

<h2>Register</h2>

@using (Html.BeginForm())
{
    <p>Username: @Html.TextBoxFor(m => m.Username)</p>
    <p>Email: @Html.TextBoxFor(m => m.Email)</p>
    <p>Password: @Html.PasswordFor(m => m.Password)</p>

    <input type="submit" value="Register" />
}

<p>@ViewBag.Message</p>

Login Feature

Login GET Method

public ActionResult Login()
{
    return View();
}

Login POST Method

[HttpPost]
public ActionResult Login(User user)
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "SELECT * FROM Users WHERE Email=@Email AND Password=@Password";
        SqlCommand cmd = new SqlCommand(query, con);

        cmd.Parameters.AddWithValue("@Email", user.Email);
        cmd.Parameters.AddWithValue("@Password", user.Password);

        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            Session["Username"] = dr["Username"].ToString();
            return RedirectToAction("Dashboard");
        }
        else
        {
            ViewBag.Message = "Invalid Email or Password";
        }
    }

    return View();
}

Explanation

  • SELECT query checks login

  • SqlDataReader reads the result

  • If user found → store username in Session

  • Redirect to Dashboard

Dashboard

public ActionResult Dashboard()
{
    if (Session["Username"] == null)
    {
        return RedirectToAction("Login");
    }

    return View();
}

Dashboard View

<h2>Welcome @Session["Username"]</h2>

<p>You have successfully logged in.</p>

@Html.ActionLink("Logout", "Logout")

Logout

public ActionResult Logout()
{
    Session.Clear();
    return RedirectToAction("Login");
}

Security Note

This example stores passwords directly.

In real applications:

  • Use password hashing

  • Use Identity Framework

  • Use authentication cookies

Final Output

User flow:

  • Register

  • Login

  • Dashboard

  • Logout

You now have a complete working login and registration system.

Why This Blog Gets High Views

  • Login system is required in every project

  • Common interview topic

  • Beginner friendly

  • Practical example

  • Real-world usage

Conclusion

In this article, we learned:

  • How to create a registration form

  • How to validate login

  • How to use Session

  • How to connect MVC with SQL Server using ADO.NET