The application uses a flexible authentication system that adapts to the environment:
- Development: Optional bypass or simple API key
- Production: Azure AD (Entra ID), Google OAuth, or API key
By default, admin endpoints are accessible without authentication in Development mode.
{
"Auth": {
"BypassInDevelopment": true
}
}To test with authentication locally:
{
"Auth": {
"BypassInDevelopment": false,
"AdminApiKey": "your-secret-dev-key-12345"
}
}X-Admin-Key: your-secret-dev-key-12345appsettings.Production.json:
{
"Auth": {
"BypassInDevelopment": false,
"AdminApiKey": "production-secret-key-from-env-variable"
}
}Set via environment variable:
export Auth__AdminApiKey="your-production-secret-key"Docker:
environment:
- Auth__AdminApiKey=your-production-secret-keyInstall package:
dotnet add package Microsoft.Identity.Webappsettings.Production.json:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "your-tenant-id",
"ClientId": "your-client-id",
"Audience": "api://your-api-id"
},
"Auth": {
"UseAzureAd": true
}
}Update Program.cs:
// Add Azure AD authentication
if (builder.Configuration.GetValue<bool>("Auth:UseAzureAd"))
{
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
}Install package:
dotnet add package Microsoft.AspNetCore.Authentication.Googleappsettings.Production.json:
{
"Authentication": {
"Google": {
"ClientId": "your-google-client-id",
"ClientSecret": "your-google-client-secret"
}
},
"Auth": {
"UseGoogle": true
}
}Update Program.cs:
if (builder.Configuration.GetValue<bool>("Auth:UseGoogle"))
{
builder.Services.AddAuthentication()
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
});
}The following endpoints require admin authentication:
- All endpoints (
POST /api/admin/puzzles,DELETE /api/admin/puzzles/{id}, etc.)
GET /api/user/all- List all users
- Any endpoint decorated with
[Authorize(Policy = "AdminOnly")]
curl http://localhost:5000/api/user/allcurl -H "X-Admin-Key: your-secret-key" http://localhost:5000/api/user/allcurl -H "Authorization: Bearer {jwt-token}" http://localhost:5000/api/user/all| Environment | Default Auth | Override |
|---|---|---|
| Development | Bypassed | Set BypassInDevelopment: false |
| Testing | Bypassed | Always bypassed for unit tests |
| Production | Required | Must configure auth |
- Never commit secrets to source control
- Use environment variables for production keys
- Rotate API keys regularly
- Use Azure KeyVault or similar for secret management
- Enable HTTPS in production (handled by reverse proxy)
- Consider rate limiting for admin endpoints
- Check
Auth:BypassInDevelopmentistruein appsettings.Development.json
- Verify environment variable is set correctly
- Check configuration binding:
Auth__AdminApiKey(double underscore)
- Ensure authentication is configured
- Check headers include correct auth token/key
- Verify user has "Admin" role