IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Model Binding and Validation in ASP.NET Core | ControllerBase vs Controller in ASP.NET Core | |
Securing ASP.NET Core Web API |
Securing your ASP.NET Web API is essential to protect sensitive data, prevent unauthorized access, and ensure reliable service. Here’s a comprehensive list of best practices tailored for ASP.NET Core Web APIs:
[ApiController]: Automatically handles model validation and returns 400 responses for invalid models.
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "your-issuer",
ValidAudience = "your-audience",
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes("your-secret-key"))
};
});
[Authorize(Roles = "Admin")] to restrict access to specific roles.[Required], [Range], [EmailAddress], etc.Restrict Origins: Configure CORS to allow only trusted domains:
services.AddCors(options => {
options.AddPolicy("AllowTrustedOrigins", builder =>
builder.WithOrigins("https://yourdomain.com")
.AllowAnyHeader()
.AllowAnyMethod());
});
X-Content-Type-Options, X-Frame-Options, and Content-Security-Policy. | Model Binding and Validation in ASP.NET Core | ControllerBase vs Controller in ASP.NET Core | |