Using a Mock OIDC / Oauth2 Server for Development and Testing
Steps
Package
Install the Nuget Package Microsoft.AspNetCore.Authentication.JwtBearer
Install Services / Middleware
In Program.cs
:
builder.Services.AddAuthentication().AddJwtBearer(options =>
{
options.RequireHttpsMetadata = !builder.Environment.IsDevelopment();
});
builder.Services.AddAuthorization();
And then:
app.UseAuthentication();
app.UseAuthorization();
In appsettings.development.json
, add a configuration called Authentication
:
"Authentication": {
"Schemes": {
"Bearer": {
"Authority": "http://localhost:8080",
"ValidAudiences": [
"default"
],
"ValidIssuer": "http://localhost:8080"
}
}
}
To start the Mock server, using Docker:
docker run -d -p 8080:8080 ghcr.io/navikt/mock-oauth2-server
Or use a docker-compose.yml
:
services:
mock-oidc:
image: ghcr.io/navikt/mock-oauth2-server:0.5.6
ports:
- 8080:8080