Asp Net Core Login Page Example

As a web developer, I have worked extensively with ASP.NET Core, and one of the essential features of any web application is a secure login page. In this article, I will walk you through an example of creating an ASP.NET Core login page from scratch, providing personal insights and detailing each step along the way.

Setting up the Environment

Before we dive into the code, let’s make sure we have the necessary tools and environment set up. To follow along, you will need:

  • .NET Core SDK installed on your machine
  • An integrated development environment (IDE) like Visual Studio or Visual Studio Code

If you haven’t already, make sure to install these prerequisites so that we can proceed smoothly with the example.

Creating a New ASP.NET Core Project

Once our environment is ready, it’s time to create a new ASP.NET Core project. Open your preferred IDE, and follow these steps:

  1. Create a new project and select the ASP.NET Core Web Application template.
  2. Choose a name for your project and click ‘Next’.
  3. Select the ‘Web Application’ template and make sure to choose ‘ASP.NET Core 3.1’ or the latest version available.
  4. Click ‘Create’ to generate the project structure.

Congratulations! You now have a basic ASP.NET Core project set up and ready for the login page implementation.

Adding the Login Page

To add the login page functionality, we need to start by setting up authentication and authorization in our ASP.NET Core application. Open the Startup.cs file, and locate the ConfigureServices method. Add the following code:

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
});

This code configures the default authentication scheme and sets up the login and access denied paths for the cookie authentication. Replace the path “/Account/Login” with the actual login page URL you want to use.

Next, let’s create the login page itself. Add a new folder called “Pages” to your project if it doesn’t already exist. Inside the “Pages” folder, create a new Razor Page called “Login.cshtml” and add the following code:

<form method="post">
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required />
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required />
</div>
<div>
<input type="submit" value="Login" />
</div>
</form>

This simple HTML form captures the user’s email and password and submits them to the server using the POST method. Feel free to add more fields or customize the form as per your requirements.

Handling the Login Request

To handle the login request and authenticate the user, we need to add a corresponding POST method to the “Login.cshtml.cs” file, which is the code-behind file for the login page. Open the “Login.cshtml.cs” file and add the following code:

public class LoginModel : PageModel
{
private readonly SignInManager<IdentityUser> _signInManager;

public LoginModel(SignInManager<IdentityUser> signInManager)
{
_signInManager = signInManager;
}

public void OnPost(string email, string password)
{
var result = _signInManager.PasswordSignInAsync(email, password, false, lockoutOnFailure: false).Result;

if (result.Succeeded)
{
// Redirect the user to the home page or a protected resource
}
else
{
// Handle failed login attempt
}
}
}

In this code, we inject the SignInManager from ASP.NET Core Identity, which handles user authentication. The OnPost method takes the email and password from the submitted form and uses PasswordSignInAsync to validate the credentials. Depending on the result, you can redirect the user to the home page or display an error message.

Conclusion

Building a secure login page is a crucial part of any web application, and ASP.NET Core provides a robust framework for implementing this feature. In this article, we walked through an example of creating an ASP.NET Core login page, from setting up the environment to handling the login request. By following these steps, you can create a fully functional login page in your ASP.NET Core application.

Remember, the example provided here is just the starting point, and there are many additional security measures and customization options you can explore. I hope this article has been helpful in getting you started with creating a login page in ASP.NET Core!