In this blog, we’ll walk through creating a simple yet effective authentication system in ASP.NET Core using sessions and cookies. By the end, you’ll have a solid understanding of how to build a custom login flow without relying on external libraries like JWT or ASP.NET Core Identity. Here’s what you’ll learn:
Sessions: How to store user data on the server to maintain authentication state.
Cookies: How to store user data on the client side for persistent authentication.
Password Hashing: How to securely hash passwords before storing or comparing them.
Login and Logout: How to implement a login system and allow users to log out securely.
Custom Authentication: How to build a lightweight authentication system tailored to your application’s needs.
This guide is perfect for beginners, junior developers, or mid-level developers looking to understand the basics of authentication in ASP.NET Core. Let’s get started! 🚀
Why Custom Login Flow?
Sometimes, you don't need the complexity of JWT or Identity for small applications. A custom login flow allows you to:
Keep things simple and lightweight.
Learn the basics of authentication (sessions, cookies, hashing).
Have full control over the authentication process.
Let’s dive in!
What We’ll Build
We’ll create a simple login system with the following features:
Login Page: Users enter their email and password.
Password Hashing: Passwords are securely hashed before being compared.
Session Management: Store user information on the server.
Cookies: Store user information on the client side.
Logout Functionality: Allow users to log out and clear their session.
Step 1: Setting Up the Project
Create a new ASP.NET Core MVC project:
Open Visual Studio or your favorite IDE.
Create a new project and select ASP.NET Core Web App (Model-View-Controller).
Name it something like CustomLoginFlow.
Add Session Support:
Open Program.cs and add the following code to enable sessions:
Step 2: Create the User Model
We need a model to represent the user’s login credentials. Create a folder named Models and add a UserModel.cs file:
Step 3: Implement Password Hashing
To securely store and compare passwords, we’ll hash them using SHA256. Create a static class DevCode with a method to hash passwords:
Step 4: Create the SignIn Controller
The SignInController handles the login logic. Create a new controller in the Controllers folder:
Step 5: Create the Home Controller
The HomeController checks if the user is authenticated before allowing access to the home page:
Step 6: Create the Views
Login Page (Views/SignIn/Index.cshtml)
Home Page (Views/Home/Index.cshtml)
Step 7: Run the Application
Run the application and navigate to the /SignIn page.
Log in with the email slh@gmail.com and password 123.
If successful, you’ll be redirected to the home page.
Click the Logout button to clear the session and cookie.
Sessions: Store user data on the server.
Cookies: Store user data on the client side.
Password Hashing: Securely hash passwords before storing or comparing them.
Logout: Clear the session and expire the cookie to log the user out.
This custom login flow is perfect for small applications or learning purposes. As your application grows, consider using ASP.NET Core Identity for more advanced features like role-based authorization, password recovery, and external logins.