---
title: "Login Form in Asp.Net MVC 4"  
description: "In this blog, I’m explaining how to create a login form in asp.net mvc 4.Step 1:Create a Login table in the database and add the values like this:  No"  
author: "Sumit Kesarwani"  
published: 2014-02-10  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/648/login-form-in-asp-dot-net-mvc-4  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 4 minutes  

---

# Login Form in Asp.Net MVC 4

In this blog, I’m explaining how to create a login [form in asp.net](https://www.mindstick.com/forum/34586/how-to-create-modal-pop-up-form-in-asp-dot-net-using-vb-dot-net) mvc 4.

Step 1:

Create a Login [table in the database](https://www.mindstick.com/forum/157515/what-are-ddl-triggers-create-a-trigger-to-prevent-a-user-to-delete-a-table-in-the-database) and add the values like this:

\
![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/3de2e12c-0b9e-4f0f-8932-3049412aa45d.png)

Now create an empty asp.net mvc 4 application and add a model class named “Login.cs” to the project and write the below code in it:

```
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Configuration;using System.Data.SqlClient;using System.Linq;using System.Web;namespace LoginFormApp.Models{    public class Login    {        [Required(ErrorMessage = "Username is required")] // make the field required        [Display(Name = "Username")]  // Set the display name of the field        public string username { get; set; }        [Required(ErrorMessage = "Password is required")]        [Display(Name = "Password")]        public string password { get; set; }        public bool checkUser(string username, string password) //This method check the user existence        {            bool flag = false;            string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // Read the connection string from the web.config file            using (SqlConnection conn = new SqlConnection(connString))            {                conn.Open();                SqlCommand cmd = new SqlCommand("Select count(*) from Login where username='" + username + "' and password='" + password + "'", conn);                flag = Convert.ToBoolean(cmd.ExecuteScalar());                return flag;            }        }    }}
```

##### Step 2:

Now add the connection string in the web.config file like this:

```
<connectionStrings>    <add name="ConnectionString" connectionString="Data source = YOUR DATA SOURCE NAME; Initial Catalog=YOUR DATABASE NAME; Integrated security=true;" providerName="System.Data.SqlClient"/>  </connectionStrings>
```

##### Step 3:

Now add a controller and named it “HomeController.cs” and write the below code like this:

```
using LoginFormApp.Models;using System;using System.Collections.Generic;using System.Configuration;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.Mvc;namespace LoginFormApp.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        [HttpGet]        public ActionResult Index()        {            return View();        }        [HttpPost]        public ViewResult Index(Login login)        {            if (ModelState.IsValid) // Check the model state for any validation errors            {                if (login.checkUser(login.username, login.password)) // Calls the Login class checkUser() for existence of the user in the database.                {                    return View("Show", login); // Return the "Show.cshtml" view if user is valid                }                else                {                    ViewBag.Message = "Invalid Username or Password";                    return View(); //return the same view with message "Invalid Username or Password"                }            }            else            {                return View(); // Return the same view with validation errors.            }        }    }}
```

##### Step 4:

Now add a view by right clicking on the Index() method (Unparameterized one) and add a view which must be a [strongly typed](https://www.mindstick.com/forum/298/strongly-typed-view-in-mvc) View like this:

![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/b4ed56c7-4838-43e4-a5f8-bd230cb81b74.png)

\

And add the below code in it:

```
@model LoginFormApp.Models.Login@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title></head><body>    <div style="margin:0 auto; text-align: center; border: 2px; border-style: solid; width:400px;background-color:bisque">        @using (Html.BeginForm())        {            <table>                <tr>                    <td>@Html.LabelFor(m => m.username)</td> @*Label to display username*@                    <td>@Html.TextBoxFor(m => m.username)</td> @*Textbox for user input*@                    <td>@Html.ValidationMessageFor(m => m.username)</td> @*Show validation error (if any) on form submission*@                </tr>                <tr>                    <td>@Html.LabelFor(m => m.password)</td> @*Label for pasword*@                    <td>@Html.PasswordFor(m => m.password)</td> @*Password box for inputting password*@                    <td>@Html.ValidationMessageFor(m => m.password)</td> @*Show validation errors(if any) on form submission*@                </tr>                <tr>                    <td></td>                    <td>                        <input type="submit" value="Submit" /></td>                </tr>                <tr>                    <td></td>                    <td>                        @ViewBag.Message                    </td>                </tr>            </table>        }    </div></body></html>
```

##### Step 5:

Now add another view to the project by right clicking on the Index() method (Parameterized one this time) and give the view name “Show.cshtml” like this:

```
@model LoginFormApp.Models.Login@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Show</title></head><body>    <div>        <h3> Hi! @Model.username</h3> @*Show the name of th euser who is logged in.*@    </div></body></html>
```

##### Output

Now run the application:

\
![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/b59209cf-6208-4d14-9c41-5c299605ce32.png)

If you click on the “Submit” button now:\

\
![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/83829f2b-aefe-4eab-a4ee-e4151e9e53f9.png)

You will see the validation [error messages](https://www.mindstick.com/forum/160245/how-to-customize-error-messages-with-a-data-annotation-in-asp-dot-net-core) shown as above:\

Now write the appropriate values in the textbox like this:

\
![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/1886cb63-eccd-44e0-94a6-41cac4e9d9cc.png)

And click on submit button, you will see the message if the user is valid like this:\

\
![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/ffd937ce-e5bf-4039-97fb-622846b9a47d.png)

And if write any wrong username or password, you have the message like this:

\
![Login Form in Asp.Net MVC 4](https://www.mindstick.com/blogs/45422e7e-6355-42e1-a5f2-b98256790167/images/4ca3ad69-8928-4110-9954-d76b3b9423a1.png)

---

Original Source: https://www.mindstick.com/blog/648/login-form-in-asp-dot-net-mvc-4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
