A Blazor Server application demonstrating session-based authentication without ASP.NET Core Identity or JWT. This project serves as a practical example of implementing basic authentication in Blazor Server apps using server-side sessions.
- Login/Logout system
- Server-side session management
- Route protection with custom AuthorizeView component
- Automatic redirect to login
- Responsive UI with Bootstrap 5
- Static SSR for login/logout (SignalR session compatibility)
- .NET 10 (LTS)
- C# 14
- Blazor Server
- Bootstrap 5
- ASP.NET Core Sessions
Note: This is a demo project. Credentials are hardcoded in the code - it does not connect to any database.
- Email: admin@example.com
- Password: 123456
AuthenticationService.LoginAsync()validates credentials against hardcoded values- On success, stores the email in server session
SessionServicemanages the session viaIHttpContextAccessor- Login/Logout pages use Static SSR (not InteractiveServer) to allow session modification
To connect to a real database, modify AuthenticationService.cs:
public Task<bool> LoginAsync(string email, string password, CancellationToken ct = default)
{
// Replace with database validation:
// var user = await _context.Users.FirstOrDefaultAsync(u => u.Email == email, ct);
// return user != null && BCrypt.Verify(password, user.PasswordHash);
if (email == "admin@example.com" && password == "123456") // <- Hardcoded (demo)
{
sessionService.SetSession(email);
return Task.FromResult(true);
}
return Task.FromResult(false);
}Modern .NET 10 architecture with:
- Primary constructors (C# 14)
- Sealed services with dependency injection
- Per-page render mode (SSR for auth, InteractiveServer for others)
- CancellationToken in async methods
- Modern .slnx solution format
- Centralized build configuration (Directory.Build.props)
- Visual Studio 2022 17.12+ or VS Code with C# Dev Kit
- .NET 10 SDK
- Modern web browser
# Clone the repository
git clone https://github.com/your-username/BlazorServerBasicAuthSession.git
# Navigate to project
cd BlazorServerBasicAuthSession
# Run the application
dotnet runThen navigate to https://localhost:7162 and login with the demo credentials.
BlazorServerBasicAuthSession/
├── Components/
│ ├── Layout/ # MainLayout, NavMenu
│ ├── Pages/ # Home, Counter, Weather, Login, Logout
│ └── Pages/Shared/ # Custom AuthorizeView
├── Services/ # AuthenticationService, SessionService
├── Models/ # LoginModel
├── Directory.Build.props
├── global.json # SDK pinning
└── BlazorServerBasicAuthSession.slnx