This repository contains a C# and ASP.NET Core application utilizing Entity Framework for database operations. This project demonstrates a basic implementation of user authentication with JWT, password hashing with BCrypt, and includes setup instructions for a PostgreSQL database.
- Introduction
- Features
- Prerequisites
- Installation
- Configuration
- Database Migration
- Running the Application
- API Endpoints
CSharp-Backend is a project showcasing the development of a backend application using C# and ASP.NET Core. The application includes user authentication, JWT integration, and database operations using Entity Framework with PostgreSQL.
- User registration and authentication
- JWT-based authentication
- Password hashing with BCrypt
- Database operations with Entity Framework Core
- PostgreSQL database integration
- API documentation with Postman
Before you begin, ensure you have the following installed:
- .NET SDK 8.0
- PostgreSQL
- Visual Studio Code (or any other code editor)
-
Clone the repository:
git clone https://github.com/NuelUzoma/CSharp-Backend.git cd CSharp-Backend
-
Install .NET SDK 8.0: Follow the instructions from the official .NET download page for debian.
For Linux:
wget https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb rm packages-microsoft-prod.deb sudo apt-get update && \ sudo apt-get install -y dotnet-sdk-8.0 sudo apt-get update && \ sudo apt-get install -y aspnetcore-runtime-8.0 dotnet --version
-
Install PostgreSQL: Follow the instructions from the official PostgreSQL download page.
-
Restore NuGet packages:
dotnet restore
-
Set up the database connection string: Update the
appsettings.json
file with your PostgreSQL connection string:{ "ConnectionStrings": { "DefaultConnection": "Host=localhost;Database=first_backend;Username=your_username;Password=your_password" }, "Jwt": { "Key": "your_256_bit_key", "Issuer": "http://localhost:5145", "Audience": "http://localhost:5145" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" }
-
Generate a 256-bit secret key for JWT:
openssl rand -base64 32
- Create and apply migrations:
dotnet dotnet-ef migrations add InitialCreate dotnet dotnet-ef database update
- Run the application:
dotnet run
- Endpoint:
POST /api/user/signup
- Description: Registers a new user.
- Request Body:
{ "name": "JohnDoe", "email": "[email protected]", "password": "your_password" }
- Response:
{ "id": 1, "name": "JohnDoe", "email": "[email protected]" }
- Endpoint:
POST /api/auth/login
- Description: Authenticates a user and returns a JWT.
- Request Body:
{ "email": "[email protected]", "password": "your_password" }
- Response:
{ "token": "your_jwt_token" }
- Endpoint:
GET /api/user
- Description: Retrieves users.
- Response:
[ { "id": 1, "name": "Emmanuel", "email": "[email protected]" }, { "id": 2, "name": "Nuel", "email": "[email protected]" }, { "id": 3, "name": "Uzo", "email": "[email protected]" }, { "id": 4, "name": "Uzoma", "email": "[email protected]" }, { "id": 5, "name": "Uzomachukwu", "email": "[email protected]" }, { "id": 6, "name": "Uzomachukws Chidera", "email": "[email protected]" }, { "id": 7, "name": "Chidera KSI", "email": "[email protected]" } ]
- Endpoint:
GET /api/user/{id}
- Description: Retrieves a user by ID.
- Response:
{ "id": 4, "name": "Uzoma", "email": "[email protected]" }
- Endpoint:
DELETE /api/user/{id}
- Description: Deletes a user by ID.
- Response:
{ "message": "User has been deleted successfully" }