Overview
SupermarketWEB implements a cookie-based authentication system using ASP.NET Core Identity. The system provides secure login, registration, and logout functionality to protect administrative features.Authentication Configuration
The authentication system is configured inProgram.cs:19-23 using cookie-based authentication:
The authentication cookie is named “MyCookieAuth” and users are automatically redirected to
/Account/Login when attempting to access protected resources.User Model
The User model (Models/User.cs:5-14) defines the structure for user accounts:
Properties
| Property | Type | Validation | Description |
|---|---|---|---|
Id | int | - | Unique identifier |
Email | string | Required, Email format | User’s email address |
Password | string | Required, Password data type | User’s password |
Authentication Flow
User Registration
New users register through the
/Account/Register page, providing email and password credentials.Login Implementation
The login process (Pages/Account/Login.cshtml.cs:28-50) validates credentials and creates authentication cookies:
Claims-Based Identity
The system creates two claims for authenticated users:- Name Claim: Set to “admin” for all authenticated users
- Email Claim: Stores the user’s email address
Registration Implementation
User registration (Pages/Account/Register.cshtml.cs:25-36) adds new users to the database:
Logout Implementation
The logout process (Pages/Account/Logout.cshtml.cs:9-13) clears the authentication cookie:
Protected Resources
Pages requiring authentication use the[Authorize] attribute:
[Authorize] attribute.
Security Features
Cookie-Based Sessions
Secure session management using encrypted cookies
Automatic Redirects
Unauthenticated users are redirected to the login page
Claims-Based Identity
User information stored in secure claims
Authorization Attributes
Simple
[Authorize] attribute protects pagesDatabase Storage
User data is stored in theUsers table via the SupermarketContext (Data/SupermarketContext.cs:17):
Best Practices
For Production Deployments:
- Implement password hashing (e.g., Identity’s PasswordHasher)
- Add password complexity requirements
- Implement account lockout after failed attempts
- Use HTTPS for all authentication endpoints
- Consider implementing two-factor authentication
- Add email verification for new registrations