Creating a login page is an essential part of any web application, allowing users to securely access their personalized content. In this article, I will guide you through the process of creating a login page in ASP.NET MVC without the need for a database. This approach is suitable for small-scale applications or when you don’t have access to a database system.
Prerequisites
Before we begin, make sure you have the following software installed on your machine:
- Visual Studio (any version)
- .NET Framework
Step 1: Create a new ASP.NET MVC Project
First, let’s create a new ASP.NET MVC project in Visual Studio. Follow these steps:
- Open Visual Studio and go to
File > New > Project. - Select the
ASP.NET Web Applicationtemplate. - Choose a name and location for your project.
- Click
OKto create the project. - In the project template selection screen, choose
Emptyand make sureMVCis selected. - Click
OKto create the project.
Step 2: Add a Login View and Controller
In this step, we will add a login view and controller to our project. Follow these steps:
- Right-click on the
Controllersfolder in the Solution Explorer and selectAdd > Controller. - Choose a name for your controller, such as
LoginController. - Click
Addto create the controller. - In the newly created
LoginController.csfile, add the following code:public class LoginController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string username, string password) { // Add your authentication logic here if (username == "myusername" && password == "mypassword") { return RedirectToAction("Dashboard", "Home"); } else { ViewBag.ErrorMessage = "Invalid username or password."; return View(); } } } - Right-click on the
Viewsfolder in the Solution Explorer and selectAdd > View. - Choose a name for your view, such as
Login. - Click
Addto create the view. - In the newly created
Login.cshtmlfile, add the following code:<h2>Login</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div> @Html.LabelFor(m => m.Username) @Html.TextBoxFor(m => m.Username) @Html.ValidationMessageFor(m => m.Username) </div> <div> @Html.LabelFor(m => m.Password) @Html.PasswordFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password) </div> <input type="submit" value="Login" /> <div> @ViewBag.ErrorMessage </div> }
Step 3: Configure Routes
Now, we need to configure the routes in our application to make sure the login page is accessible. Follow these steps:
- Open the
RouteConfig.csfile located in theApp_Startfolder. - Add the following code inside the
RegisterRoutesmethod:routes.MapRoute( name: "Login", url: "login", defaults: new { controller = "Login", action = "Index" } );
Step 4: Test the Login Page
Now, let’s test our login page. Follow these steps:
- Press
F5or click on theDebugbutton in Visual Studio to run the application. - Open your web browser and navigate to https://localhost:port/login (replace
portwith the appropriate port number). - You should see the login page. Enter
myusernameas the username andmypasswordas the password. - If the credentials are correct, you will be redirected to the dashboard page. Otherwise, an error message will be displayed.
Conclusion
In this article, we learned how to create a login page in ASP.NET MVC without the need for a database. Although this approach is suitable for small-scale applications, it is important to note that using a database for authentication is more secure and scalable. However, for quick prototyping or when a database is not available, this method can be useful. Feel free to experiment and enhance the code to fit your specific requirements.

