Skip to content

Web application developed in Blazor Server that implements a basic authentication system using server sessions. This project serves as a practical example of how to implement authentication in Blazor Server applications without relying on Identity or JWT.

License

Notifications You must be signed in to change notification settings

scopweb/BlazorServerBasicAuthSession

Repository files navigation

Blazor Server Authentication Demo

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.

Features

  • 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)

Tech Stack

  • .NET 10 (LTS)
  • C# 14
  • Blazor Server
  • Bootstrap 5
  • ASP.NET Core Sessions

Authentication

Note: This is a demo project. Credentials are hardcoded in the code - it does not connect to any database.

Demo Credentials

How It Works

  1. AuthenticationService.LoginAsync() validates credentials against hardcoded values
  2. On success, stores the email in server session
  3. SessionService manages the session via IHttpContextAccessor
  4. Login/Logout pages use Static SSR (not InteractiveServer) to allow session modification

For Production

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);
}

Architecture

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)

Prerequisites

  • Visual Studio 2022 17.12+ or VS Code with C# Dev Kit
  • .NET 10 SDK
  • Modern web browser

Getting Started

# Clone the repository
git clone https://github.com/your-username/BlazorServerBasicAuthSession.git

# Navigate to project
cd BlazorServerBasicAuthSession

# Run the application
dotnet run

Then navigate to https://localhost:7162 and login with the demo credentials.

Project Structure

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

About

Web application developed in Blazor Server that implements a basic authentication system using server sessions. This project serves as a practical example of how to implement authentication in Blazor Server applications without relying on Identity or JWT.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors