How To Create Login Page In Asp.net Mvc Without Database

How To Articles

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:

  1. Open Visual Studio and go to File > New > Project.
  2. Select the ASP.NET Web Application template.
  3. Choose a name and location for your project.
  4. Click OK to create the project.
  5. In the project template selection screen, choose Empty and make sure MVC is selected.
  6. Click OK to 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:

  1. Right-click on the Controllers folder in the Solution Explorer and select Add > Controller.
  2. Choose a name for your controller, such as LoginController.
  3. Click Add to create the controller.
  4. In the newly created LoginController.cs file, 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();
              }
          }
      }    
      
  5. Right-click on the Views folder in the Solution Explorer and select Add > View.
  6. Choose a name for your view, such as Login.
  7. Click Add to create the view.
  8. In the newly created Login.cshtml file, 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:

  1. Open the RouteConfig.cs file located in the App_Start folder.
  2. Add the following code inside the RegisterRoutes method:
    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:

  1. Press F5 or click on the Debug button in Visual Studio to run the application.
  2. Open your web browser and navigate to https://localhost:port/login (replace port with the appropriate port number).
  3. You should see the login page. Enter myusername as the username and mypassword as the password.
  4. 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.