A RESTful Web API for managing library books with JWT-based authentication built with ASP.NET Core.
- User registration and login
- JWT-based authentication
- CRUD operations for books (Create, Read, Update, Delete)
- Search books by title or author
- Entity Framework Core with MSSQL
- Database seeding with sample data
- Clean architecture with layered design
- Comprehensive error handling
- Swagger UI for API documentation
- Framework: ASP.NET Core 8.0
- Database: Microsoft SQL Server
- ORM: Entity Framework Core
- Authentication: JWT Bearer Token
- Documentation: Swagger/OpenAPI
- .NET 8.0 SDK
- SQL Server or SQL Server LocalDB
- Visual Studio 2022 or Visual Studio Code
git clone https://github.com/Juninhotech/LibraryManagementAPI.git
cd LibraryManagementAPIUpdate the connection string in appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=LibraryManagementDb;Trusted_Connection=true;TrustServerCertificate=true;"
}For SQL Server:
"DefaultConnection": "Server=localhost;Database=LibraryManagementDb;User Id=sa;Password=YourPassword;TrustServerCertificate=true;"dotnet restoredotnet ef migrations add InitialCreate
dotnet ef database updateNote: The database will be automatically seeded with sample books on the first run.
dotnet runThe API will start at:
- HTTP:
http://localhost:5226
- Navigate to
http://localhost:5226/swagger - Test endpoints directly from the browser
POST https://localhost:5226/api/auth/register
Content-Type: application/json
{
"username": "testuser",
"email": "test@example.com",
"password": "Password123!"
}Response:
{
"username": "testuser",
"email": "test@example.com"
}POST https://localhost:5226/api/auth/login
Content-Type: application/json
{
"token": "eyJhbGciOiJIUzI1NiIs...",
"username": "testuser",
"password": "Password123!"
}GET https://localhost:5226/api/books
Authorization: Bearer <your-jwt-token>GET https://localhost:5226/api/books?search=clean
Authorization: Bearer <your-jwt-token>GET https://localhost:5226/api/books/1
Authorization: Bearer <your-jwt-token>POST https://localhost:5226/api/books
Authorization: Bearer <your-jwt-token>
Content-Type: application/json
{
"title": "Domain-Driven Design",
"author": "Eric Evans",
"isbn": "978-0321125217",
"publishedDate": "2003-08-20T00:00:00"
}PUT https://localhost:5226/api/books/1
Authorization: Bearer <your-jwt-token>
Content-Type: application/json
{
"title": "Clean Code - Updated",
"author": "Robert C. Martin",
"isbn": "978-0132350884",
"publishedDate": "2008-08-01T00:00:00"
}DELETE https://localhost:5226/api/books/1
Authorization: Bearer <your-jwt-token>| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/auth/register |
Register new user | No |
| POST | /api/auth/login |
Login user | No |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/books |
Get all books | Yes |
| GET | /api/books?search={term} |
Search books | Yes |
| GET | /api/books/{id} |
Get book by ID | Yes |
| POST | /api/books |
Create new book | Yes |
| PUT | /api/books/{id} |
Update book | Yes |
| DELETE | /api/books/{id} |
Delete book | Yes |
- Register a new user or login with existing credentials
- Receive a JWT token in the response
- Include the token in the
Authorizationheader for all protected endpoints:Authorization: Bearer <your-jwt-token>
Id(int, Primary Key)Title(nvarchar(200), Required)Author(nvarchar(100), Required)ISBN(nvarchar(20), Required, Unique)PublishedDate(datetime2)
Id(int, Primary Key)Username(nvarchar(50), Required, Unique)Email(nvarchar(100), Required, Unique)PasswordHash(nvarchar(max), Required)CreatedAt(datetime2)
The database is seeded with 5 programming books:
- Clean Code - Robert C. Martin
- The Pragmatic Programmer - Andrew Hunt and David Thomas
- Design Patterns - Gang of Four
- C# in Depth - Jon Skeet
- Refactoring - Martin Fowler
The API returns appropriate HTTP status codes:
200 OK- Successful GET/PUT requests201 Created- Successful POST requests204 No Content- Successful DELETE requests400 Bad Request- Invalid input data401 Unauthorized- Missing or invalid token404 Not Found- Resource not found500 Internal Server Error- Server errors
- Passwords are hashed using Bcrypt
- JWT tokens expire after 60 mins (configurable)
- All book endpoints require authentication
- SQL injection protection via EF Core parameterized queries
- Verify SQL Server is running
- Check connection string in
appsettings.json - Ensure database exists or migrations have been applied
- Ensure the token is included in the Authorization header
- Check token expiration (default 24 hours)
- Verify the JWT secret key matches in configuration
- The project follows Repository and Service patterns for clean separation of concerns
- Dependency injection is used throughout the application
- The API uses DTOs to separate domain models from API contracts
- Comprehensive logging is implemented for debugging
- Add pagination for book listing
- Implement role-based authorization (Admin/User)
- Add book categories and tags
- Implement book borrowing system
- Add unit and integration tests
- Implement refresh tokens
- Add API rate limiting