Overview
TheUser model handles authentication and user account management for the SupermarketWEB application. It stores user credentials with validation attributes to ensure proper email format and secure password handling.
Class Definition
Properties
Primary key and unique identifier for the user account. Auto-generated by the database.
User’s email address used for authentication and identification. Must be a valid email format.Data Annotations:
[Required]- Email is mandatory for account creation[DataType(DataType.EmailAddress)]- Provides email-specific validation and UI hints
User’s password for authentication. Should be hashed before storage.Data Annotations:
[Required]- Password is mandatory for account creation[DataType(DataType.Password)]- Ensures password input fields render with masking
Data Annotations
The User model uses two critical data annotation attributes:Required Attribute
- Ensures the field cannot be null or empty
- Generates client-side and server-side validation
- Returns validation error if field is missing
DataType Attribute
The
DataType attribute doesn’t perform actual validation; it provides UI rendering hints and metadata. Use additional validation attributes like [EmailAddress] for strict email validation.Security Considerations
CRITICAL: Never store passwords in plain text. Always hash passwords using a secure algorithm before saving to the database.
Password Hashing Example
Using ASP.NET Core Identity (Recommended)
For production applications, use ASP.NET Core Identity:Usage Examples
Creating a New User
User Authentication
Checking if Email Exists
Updating User Email
Changing Password
Password Reset
Database Schema
When migrated to the database, the User table has the following structure:| Column | Type | Nullable | Key |
|---|---|---|---|
| Id | int | No | Primary Key (Identity) |
| nvarchar(MAX) | No | ||
| Password | nvarchar(MAX) | No |
Add a unique index on the Email field to prevent duplicate accounts:
Enhanced Model with Validation
For production use, enhance the model with additional validation:Extended User Model
For a complete user management system, consider extending the model:Authentication Context
The User model works within the authentication flow:- Registration: Create new user with hashed password
- Login: Verify credentials against stored hash
- Session Management: Maintain authenticated state
- Authorization: Check user permissions based on role
- Password Reset: Secure token-based password recovery
Best Practices
- Password Hashing: Always use bcrypt, PBKDF2, or Argon2 for password hashing
- Salt Passwords: Use unique salts for each password hash
- Email Uniqueness: Enforce unique constraint on email field
- Password Complexity: Require minimum length, special characters, numbers
- Account Lockout: Implement lockout after failed login attempts
- Email Verification: Require email confirmation before activation
- Two-Factor Authentication: Consider implementing 2FA for enhanced security
- Password History: Prevent reuse of recent passwords
- Session Management: Implement secure session handling with timeouts
- Audit Logging: Track authentication events and suspicious activities
Security Checklist
- Passwords are hashed using secure algorithm (bcrypt/Argon2)
- Email addresses are validated and normalized
- Unique constraint on email field
- Password complexity requirements enforced
- Account lockout after failed attempts
- Email verification implemented
- Secure password reset flow
- HTTPS enforced for all authentication endpoints
- Protection against brute force attacks
- Regular security audits
Related Models
- Data Models Overview - Complete entity relationship diagram
- Customer Model - Customer information (separate from user authentication)