A comprehensive ASP.NET Core MVC application for managing employees with role-based access control, claims-based authorization, and external authentication providers.
- Employee Management: Create, read, update, and delete employee records
- User Authentication: Local account registration and login with email confirmation
- External Authentication: Sign in with Google and Facebook
- Role-Based Access Control: Admin, Super Admin role management
- Claims-Based Authorization: Fine-grained permissions using custom claims
- Password Management: Change password, reset password, add password for external login users
- Account Lockout: Automatic lockout after failed login attempts
- Data Protection: Encrypted employee IDs in URLs
- Photo Upload: Employee profile photo management
- Email confirmation required for new accounts
- Custom email confirmation token provider with extended lifespan
- Account lockout after 5 failed attempts (15-minute duration)
- Password requirements: Minimum 8 characters, 2 unique characters
- Claims-based authorization for administrative actions
- Custom authorization handlers for role management
- Framework: ASP.NET Core 10.0 (MVC)
- Database: SQL Server with Entity Framework Core
- Authentication: ASP.NET Core Identity
- External Auth: Google OAuth 2.0, Facebook Login
- Logging: NLog
- Frontend: Bootstrap 5, Razor Views
- Security: Data Protection API, Custom Authorization Handlers
- .NET 10.0 SDK or later
- SQL Server (LocalDB, Express, or full version)
- Visual Studio Code or Visual Studio 2022
- Google Cloud Console account (for Google authentication)
- Facebook Developer account (for Facebook authentication)
- Clone the Repository
git clone <repository-url>
cd EmployeeManagement- Setup User Secrets This application uses User Secrets to store sensitive configuration data. Never commit secrets to source control. Initialize User Secrets
dotnet user-secrets initAdd Database Connection String
dotnet user-secrets set "ConnectionStrings:EmployeeDBConnection" "Server=(localdb)\\mssqllocaldb;Database=EmployeeDB;Trusted_Connection=true;MultipleActiveResultSets=true"For SQL Server Express, use:
dotnet user-secrets set "ConnectionStrings:EmployeeDBConnection" "Server=localhost\\SQLEXPRESS;Database=EmployeeDB;Trusted_Connection=true;MultipleActiveResultSets=true"For SQL Server with credentials:
dotnet user-secrets set "ConnectionStrings:EmployeeDBConnection" "Server=your-server;Database=EmployeeDB;User Id=your-username;Password=your-password;MultipleActiveResultSets=true"Add Google Authentication Credentials
- Go to Google Cloud Console
- Create a new project or select an existing one
- Go to Credentials → Create Credentials → OAuth 2.0 Client ID
- Set application type to "Web application"
- Add authorized redirect URI: http://localhost:5181/signin-google
- Copy the Client ID and Client Secret
dotnet user-secrets set "Authentication:Google:ClientId" "your-google-client-id"
dotnet user-secrets set "Authentication:Google:ClientSecret" "your-google-client-secret"Add Facebook Authentication Credentials
- Go to Facebook Developers
- Create a new app or select an existing one
- Add Facebook Login product
- Go to Settings → Basic
- Add platform: Website
- Set Site URL: https://localhost:5181
- In Facebook Login Settings, add Valid OAuth Redirect URI: https://localhost:5181/signin-facebook
- Copy the App ID and App Secret
dotnet user-secrets set "Authentication:Facebook:AppId" "your-facebook-app-id"
dotnet user-secrets set "Authentication:Facebook:AppSecret" "your-facebook-app-secret"Verify User Secrets
dotnet user-secrets listYou should see:
Authentication:Facebook:AppId = your-facebook-app-id
Authentication:Facebook:AppSecret = your-facebook-app-secret
Authentication:Google:ClientId = your-google-client-id
Authentication:Google:ClientSecret = your-google-client-secret
ConnectionStrings:EmployeeDBConnection = Server=...;Database=EmployeeDB;...- Database Setup Apply Migrations The project uses Entity Framework Core Code-First approach. Apply migrations to create the database:
# Restore NuGet packages
dotnet restore
# Apply migrations and create database
dotnet ef database updateIf you don't have the dotnet-ef tool installed:
dotnet tool install --global dotnet-efCreate Migrations (Optional - for development) If you need to create new migrations after model changes:
# Add a new migration
dotnet ef migrations add YourMigrationName
# Apply the migration
dotnet ef database update
# Remove last migration (if not applied)
dotnet ef migrations removeSeed Initial Data (Manual) After the database is created, you'll need to manually create:
-
Admin User:
- Register a new user through the application
- Manually add to Admin role using SQL or create through code
-
Initial Roles:
INSERT INTO AspNetRoles (Id, Name, NormalizedName, ConcurrencyStamp)
VALUES
(NEWID(), 'Admin', 'ADMIN', NEWID()),
(NEWID(), 'Super Admin', 'SUPER ADMIN', NEWID());-
Claims (Available claims are defined in Models/ClaimsStore.cs):
- Create Role
- Edit Role
- Delete Role
-
Run the Application
dotnet run
Or using Visual Studio:
- Press `F5` or click "Run"
The application will be available at:
- HTTPS: `https://localhost:5001`
- HTTP: `http://localhost:5000`
## Project Structure
EmployeeManagement/
├── Controllers/
│ ├── AccountController.cs # Authentication & user management
│ ├── AdministrationController.cs # Role & claims management
│ └── HomeController.cs # Employee CRUD operations
|
├── Models/
│ ├── ApplicationUser.cs # Extended Identity user
│ ├── Employee.cs # Employee entity
│ ├── AppDbContext.cs # Database context
│ ├── IEmployeeRepository.cs # Employee Interface
│ ├── SQLEmployeeRepository.cs # Implementation of the IEmployeeRepository interface
│ └── ClaimsStore.cs # Available claims
|
├── ViewModels/ # View models for forms
├── Security/
│ ├── CustomEmailConfirmationTokenProvider.cs
│ ├── DataProtectionPurposeStrings.cs
│ └── Authorization Handlers/
├── Views/ # Razor views
├── wwwroot/ # Static files
└── Program.cs # Application configurationThe application uses three types of service lifetimes: Scoped Services (One instance per HTTP request):
services.AddScoped<IEmployeeRepository, SQLEmployeeRepository>();- Database contexts
- Repositories
- Per-request state
Singleton Services (One instance for entire application):
services.AddSingleton<IAuthorizationHandler, CanEditOnlyOtherAdminRolesAndClaimsHandler>();
services.AddSingleton<DataProtectionPurposeStrings>();- Authorization handlers
- Configuration services
- Stateless services
Transient Services (New instance every time):
- Used for lightweight, stateless services (not currently used in this project)
Defined in Program.cs:
- DeleteRolePolicy: Requires "Delete Role" claim with value "true"
- EditRolePolicy: Custom requirement - Admin with Edit Role claim, or Super Admin
- AdminRolePolicy: Requires Admin role
- TestRolePolicy: Requires both Delete Role and Create Role claims
options.Password.RequiredLength = 8;
options.Password.RequiredUniqueChars = 2;
options.SignIn.RequireConfirmedEmail = true;
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);- Email Confirmation Token: 3 days
- Default Tokens (password reset, etc.): 5 hours
- Navigate to /Account/Register
- Fill in email, password, and city
- Check email for confirmation link
- Click confirmation link to activate account
- Login at /Account/Login
- Click "Google" or "Facebook" button on login page
- Authorize the application
- If first time: Account is automatically created
- If email not confirmed: Confirmation link is logged (check console/logs)
List Employees: Navigate to home page
Create Employee: Click "Create" → Fill form → Upload photo (optional)
View Details: Click employee name
Edit Employee: Click "Edit" on details page
Delete Employee: Not implemented (can be added)
List Users: Navigate to /Administration/ListUsers
Edit User: Click "Edit" → Update email, username, city
Manage Roles: Click "Manage Roles" → Select/deselect roles
Manage Claims: Click "Manage Claims" → Select/deselect claims
Delete User: Click "Delete" → Confirm
List Roles: Navigate to /Administration/ListRoles
Create Role: Click "Create Role" → Enter role name
Edit Role: Click role name → Update name and users
Delete Role: Click "Delete" (requires Delete Role claim)
Change Password: /Account/ChangePassword (for users with local password)
Add Password: /Account/AddPassword (for external login users)
Forgot Password: Click "Forgot Password" on login page
Reset Password: Use link from forgot password email
Database Connection Issues Error: "Cannot open database 'EmployeeDB'" Solution:
# Check if SQL Server is running
# Verify connection string in user secrets
dotnet user-secrets list
# Recreate database
dotnet ef database drop
dotnet ef database updateEmail Confirmation Link Not Working Issue: Confirmation links are logged to console in development Solution:
- Check application logs/console output for the confirmation link
- In production, configure an email service (SMTP, SendGrid, etc.)
- Implement IEmailService to send actual emails
External Login Not Working Google/Facebook returns error:
- Verify redirect URIs in provider console match exactly
- Check client ID and secret in user secrets
- Ensure application is running on HTTPS
- Check that APIs are enabled in Google Cloud Console
Authorization Issues Access Denied even as Admin:
- Verify user has the required claims:
SELECT * FROM AspNetUserClaims WHERE UserId = 'your-user-id'- Check policy requirements in Program.cs
- Verify authorization handler logic
Migration Issues Error: "No migrations found" Solution:
# Create initial migration
dotnet ef migrations add InitialCreate
# Apply migration
dotnet ef database update- Models: Add to
/Modelsdirectory - ViewModels: Add to
/ViewModelsfor form binding - Controllers: Add to
/Controllers - Views: Add to
/Views/{ControllerName} - Update DbContext: Add
DbSet<YourModel>if needed - Create Migration:
dotnet ef migrations add YourFeatureName - Update Database:
dotnet ef database update
- Use dependency injection for all services
- Follow repository pattern for data access
- Use view models for form submission
- Implement proper validation (both client and server side)
- Handle errors gracefully with try-catch blocks
- Log important operations using ILogger
✅ Implemented:
- User secrets for sensitive data
- Email confirmation required
- Account lockout after failed attempts
- Password requirements enforced
- Claims-based authorization
- Data protection for sensitive IDs
- HTTPS enforcement (production)
- CSRF protection (built-in)
- Configure real email service (SendGrid, AWS SES, etc.)
- Enable two-factor authentication
- Implement rate limiting
- Add CAPTCHA for registration/login
- Set up proper logging and monitoring
- Use a secrets manager (Azure Key Vault, AWS Secrets Manager)
- Configure HTTPS certificate
- Enable HSTS (HTTP Strict Transport Security)
This project is for educational purposes from the Kudvenkat dotnet core series. Modify as needed for your use case. Support