This project demonstrates how to implement JWT (JSON Web Token) authorization in a .NET Core Web API 6 application. It includes the setup for secure endpoints using AddSecurityRequirement
, AddSecurityDefinition
, and service authorization.
- JWT Authentication: Secure your API endpoints using JWT tokens.
- Swagger Integration: Integrated Swagger UI with JWT authorization configuration for easy testing of secured endpoints.
- Role-Based Authorization: Implement role-based access control to restrict access to specific parts of the API.
- Token Validation: Validate JWT tokens to ensure authenticity and integrity.
-
Clone the repository:
git clone https://github.com/your-username/jwt-authorization-dotnet-core-webapi6.git
-
Navigate to the project directory:
cd jwt-authorization-dotnet-core-webapi6
-
Restore the dependencies:
dotnet restore
-
Build the project:
dotnet build
-
Run the application:
dotnet run
-
JWT Settings: Update the
appsettings.json
file with your JWT settings:"Jwt": { "Key": "your_secret_key", "Issuer": "your_issuer", "Audience": "your_audience", "Subject": "your_subject" }
-
Swagger Configuration: The
Startup.cs
orProgram.cs
file is configured to add JWT authentication to Swagger:services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "JWT Authorization API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.ApiKey, Scheme = "Bearer", BearerFormat = "JWT", In = ParameterLocation.Header, Description = "JWT Authorization header using the Bearer scheme." }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] {} } }); });
- Generate Token: Use the
/api/auth/login
endpoint to generate a JWT token by providing valid user credentials. - Authorize Requests: Include the generated token in the
Authorization
header with theBearer
scheme to access secured endpoints. - Testing with Swagger: Use the Swagger UI to test the API endpoints. Click on the
Authorize
button and enter the JWT token to authenticate.
Feel free to submit issues or pull requests if you have any improvements or fixes.