A .NET Web API that demonstrates role based authorization with JSON Web Tokens (JWT). Users authenticate with email and password, receive a signed token that carries their role, and the token controls which employee (Funcionario) endpoints they can call.
- C# on .NET 7
- ASP.NET Core Web API with controllers
- JWT bearer authentication (Microsoft.AspNetCore.Authentication.JwtBearer)
- Entity Framework Core 7 with SQL Server (LocalDB)
- AutoMapper for request to entity mapping
- Swagger / OpenAPI (Swashbuckle) for interactive documentation
- Authenticates an employee by email and password and returns a signed JWT (
POST /api/Autenticacao). - Signs tokens with HMAC SHA-512 and embeds the employee name, email and role as claims. Tokens expire five minutes after issue.
- Protects the employee endpoints with role based policies using three roles:
Funcionario,GerenteandAdministrador. - Exposes CRUD endpoints for employees:
GET /api/FuncionarioandGET /api/Funcionario/{id}: any of the three roles.POST /api/FuncionarioandPUT /api/Funcionario/{id}:GerenteorAdministrador.DELETE /api/Funcionario/{id}:Administradoronly.
- Ships EF Core migrations that create the schema and seed the three roles plus one sample user per role.
- Serves a Swagger UI with a Bearer token input so requests can be tested from the browser.
The initial data migration inserts three roles and one sample account for each. The passwords below come from the seed migration and are for local testing only.
| Role | Password | |
|---|---|---|
| Funcionario | funcionario@glass.com.br | funcionario123 |
| Gerente | gerente@glass.com.br | gerente123 |
| Administrador | adm@glass.com.br | adm123 |
Requirements: the .NET 7 SDK and a SQL Server LocalDB instance (the default connection string points at (localdb)\MSSQLLocalDB).
git clone https://github.com/nicolasfvp/jwt-authorization-api.git
cd jwt-authorization-api/ExercicioSemana2
# create the database from the EF Core migrations
dotnet tool install --global dotnet-ef
dotnet ef database update
# run the API
dotnet runWith the development profile the API listens on http://localhost:5197 and https://localhost:7240, and the Swagger UI is available at /swagger.
Typical flow:
- Call
POST /api/Autenticacaowith a JSON body{ "email": "...", "senha": "..." }to get a token. - Send that token as an
Authorization: Bearer <token>header on the employee endpoints.
Database connection and JWT settings live in ExercicioSemana2/appsettings.json under ConnectionStrings:ServerConnection and the Jwt section (Issuer, Audience, Key). Replace the sample signing key before using this anywhere other than a local machine.
ExercicioSemana2/
Controllers/ Authentication and employee endpoints
Services/ Token generation (ITokenService, TokenService)
Models/ Funcionario and Permissao entities
Dto/ Request and login payloads
Context/ EF Core DbContext
Migrations/ Schema and seed data
Personal study project built while learning JWT authentication and authorization in ASP.NET Core. The project folder and namespace keep their original name, ExercicioSemana2.
Released under the MIT License. See the LICENSE file.