PolicyAuthWebAPI
is an ASP.NET Core Web API project designed to manage user accounts with JWT-based authentication and custom authorization policies. It includes features for user registration, login, and role-based access control.
- User Registration: Allows new users to register with email, password, role, and date of birth.
- User Login: Authenticates users and generates JWT tokens.
- Role-Based Authorization: Custom policies for different user roles.
- Password Management: Password complexity requirements enforced by ASP.NET Core Identity.
- .NET 8 SDK or later
- SQL Server or another supported database
- Postman or Curl for testing API endpoints
-
Clone the Repository
git clone https://github.com/yourusername/PolicyAuthWebAPI.git cd PolicyAuthWebAPI
-
Install Dependencies
Make sure you have the necessary packages installed by running:
dotnet restore
-
Configure the Database
Update the connection string in
appsettings.json
to match your database configuration:"ConnectionStrings": { "Default": "Server=your_server;Database=your_db;User Id=your_user;Password=your_password;" }
-
Run Migrations
Apply database migrations:
dotnet ef database update
-
Run the Application
Start the API:
dotnet run
-
Endpoint:
POST /account/create
-
Description: Registers a new user.
-
Request Body:
{ "Email": "user@example.com", "Password": "Password123!", "Role": "admin", "DateOfBirth": "2000-01-01T00:00:00" }
-
Responses:
200 OK
: User registered successfully.400 Bad Request
: Invalid request or failed registration.
-
Endpoint:
POST /account/login
-
Description: Authenticates a user and returns a JWT token.
-
Request Body:
{ "email": "user@example.com", "password": "Password123!" }
-
Responses:
200 OK
: JWT token returned.404 Not Found
: User not found.400 Bad Request
: Invalid credentials.
- Endpoint:
GET /list
- Description: Accessible to users with
admin
ormanager
roles.
- Description: Accessible to users with
- Endpoint:
GET /single
- Description: Accessible to users with
admin
oruser
roles.
- Description: Accessible to users with
- Endpoint:
GET /home
- Description: Accessible to users with
admin
,manager
, oruser
roles.
- Description: Accessible to users with
Use Postman or Curl to test the API endpoints. Ensure that you have a valid JWT token for protected endpoints.
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes and push to your fork.
- Create a pull request to the main repository.
© 2024 Ahmad Hamarsheh. All rights reserved.