A sample ASP.NET Core 3.1 Web API for lodging management that uses JWT authentication and ASP.NET Identity backed by in-memory databases.
This solution demonstrates:
- ASP.NET Core 3.1 Web API
- JWT Bearer token authentication
- ASP.NET Core Identity with role-based authorization
- In-memory Entity Framework Core databases for both Identity and application data
- A simple accommodation (
Acomodacao) management API
HospedagemAPI/- API host projectBusiness/- application business logic, security helpers, and servicesDomain/- domain modelsPersistence/- EF Core database contexts
/loginendpoint for user authentication- JWT token issuance with RSA signing and configured issuer/audience
- Protected
/acomodacoesendpoints requiringBearerauthentication - In-memory data persistence for quick local testing
- Initial seeded users and roles on startup
- .NET Core SDK 3.1
- Visual Studio 2019 or later /
dotnetCLI
-
Open the solution in Visual Studio or use the CLI:
cd c:\Projects\DotNet\HospedagemAPI-JWT-Identity dotnet restore dotnet build
-
Run the API project:
dotnet run --project HospedagemAPI\HospedagemAPI.csproj -
The API starts on the configured host and port. By default, HTTPS is enabled.
POST /login
Request body example:
{
"UserID": "admin_hospedagemAPI",
"Password": "AdminHospedagemAPI01!"
}Successful response example:
{
"authenticated": true,
"created": "2026-05-28 12:34:56",
"expiration": "2026-05-28 12:35:56",
"accessToken": "<jwt-token>",
"refreshToken": null,
"message": "Ok"
}GET /acomodacoes
- Requires
Authorization: Bearer <token>header - Returns all accommodations ordered by name
POST /acomodacoes
- Requires
Authorization: Bearer <token>header - Accepts an
AcomodacaoJSON payload
Example request body:
{
"Nome": "Suíte Luxo",
"Tipo": "Quarto",
"Diaria": 250.00,
"Area": "35m²",
"Local": "Centro"
}Response is a Resultado object containing validation information.
The application seeds an in-memory Identity database at startup with:
-
Valid admin user:
UserName:admin_hospedagemAPIPassword:AdminHospedagemAPI01!- Role:
Acesso-HospedagemAPI
-
Invalid user (no access role):
UserName:usrinvalido_hospedagemAPIPassword:UsrInvHospedagemAPI01!
Only the role-bound admin user can successfully authenticate and access the protected API endpoints.
Configured in HospedagemAPI/appsettings.json:
Audience:Clients-HospedagemAPIIssuer:HospedagemAPISeconds:60
- The project uses
Microsoft.EntityFrameworkCore.InMemoryfor easy testing and does not persist data between runs. - Token expiration is short by default; adjust
Secondsinappsettings.jsonfor longer-lived tokens. - The API currently does not expose registration or refresh-token endpoints.