This project demonstrates how to implement authentication and authorization in a Blazor application. It includes a .NET Core Web API for user management and JWT token-based authentication.
- User registration and login
- JWT token generation and validation
- Role-based authorization
- Secure API endpoints
- .NET 8 SDK
- Visual Studio 2019 or later with ASP.NET and web development workload
- SQL Server or SQL Server Express
-
Clone the repository:
git clone https://github.com/Ibraheem-MuhammedAmeen/Authentication-and-Authorization-With-Blazor.git cd Authentication-and-Authorization-With-Blazor -
Set up the database:
- Update the connection string in
appsettings.jsonto point to your SQL Server instance. - Apply migrations to create the database schema.
dotnet ef database update
- Update the connection string in
-
Run the application:
dotnet run
The API will be available at
https://localhost:5001.
- Server: Contains the ASP.NET Core Web API for user management and JWT token generation.
- Client: Contains the Blazor WebAssembly application for user registration, login, and authorization.
POST /api/accounts/create: Register a new userPOST /api/accounts/login: Authenticate a user and return a JWT tokenGET /api/protected: A protected endpoint accessible only to authenticated users
-
Register a new user:
Send a
POSTrequest to/api/accounts/createwith the following JSON payload:{ "email": "user@example.com", "password": "P@ssword123" } -
Login:
Send a
POSTrequest to/api/accounts/loginwith the following JSON payload:{ "email": "user@example.com", "password": "P@ssword123" }You will receive a JWT token in the response.
-
Access protected endpoints:
Include the JWT token in the
Authorizationheader of your requests:Authorization: Bearer your-jwt-token
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.