diff --git a/DD.Persistence.API/DependencyInjection.cs b/DD.Persistence.API/DependencyInjection.cs index 0b0ee72..b5068cc 100644 --- a/DD.Persistence.API/DependencyInjection.cs +++ b/DD.Persistence.API/DependencyInjection.cs @@ -74,16 +74,16 @@ public static class DependencyInjection private static void AddKeyCloakAuthentication(this IServiceCollection services, IConfiguration configuration) { - var keyCloakHost = configuration["Authentication:Audience"]; + var keyCloakHost = configuration["KeyCloakAuthentication:Host"]; services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; - options.Audience = configuration["Authentication:Audience"]; + options.Audience = configuration["KeyCloakAuthentication:Audience"]; options.MetadataAddress = $"{keyCloakHost}/.well-known/openid-configuration"; options.TokenValidationParameters = new TokenValidationParameters { - ValidIssuer = keyCloakHost, + ValidIssuer = keyCloakHost }; }); } @@ -144,6 +144,8 @@ public static class DependencyInjection #region Keycloak private static void AddKeycloakSecurity(this SwaggerGenOptions options, IConfiguration configuration) { + var keyCloakHost = configuration["KeyCloakAuthentication:Host"]; + options.AddSecurityDefinition("Keycloak", new OpenApiSecurityScheme { Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345token'", @@ -154,7 +156,7 @@ public static class DependencyInjection { Implicit = new OpenApiOAuthFlow { - AuthorizationUrl = new Uri(configuration["Authentication:AuthorizationUrl"]!), + AuthorizationUrl = new Uri($"{keyCloakHost}/protocol/openid-connect/auth"), } } }); diff --git a/DD.Persistence.App/appsettings.json b/DD.Persistence.App/appsettings.json index bec1541..9b3a54f 100644 --- a/DD.Persistence.App/appsettings.json +++ b/DD.Persistence.App/appsettings.json @@ -9,12 +9,11 @@ "DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", - "Authentication": { - "Audience": "account", - "ValidIssuer": "http://192.168.0.10:8321/realms/Persistence", - "AuthorizationUrl": "http://192.168.0.10:8321/realms/Persistence/protocol/openid-connect/auth" - }, "NeedUseKeyCloak": false, + "KeyCloakAuthentication": { + "Audience": "account", + "Host": "http://192.168.0.10:8321/realms/Persistence" + }, "AuthUser": { "username": "myuser", "password": 12345, diff --git a/DD.Persistence.App/defaultsettings.json b/DD.Persistence.App/defaultsettings.json index bec1541..9b3a54f 100644 --- a/DD.Persistence.App/defaultsettings.json +++ b/DD.Persistence.App/defaultsettings.json @@ -9,12 +9,11 @@ "DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=q;Persist Security Info=True" }, "AllowedHosts": "*", - "Authentication": { - "Audience": "account", - "ValidIssuer": "http://192.168.0.10:8321/realms/Persistence", - "AuthorizationUrl": "http://192.168.0.10:8321/realms/Persistence/protocol/openid-connect/auth" - }, "NeedUseKeyCloak": false, + "KeyCloakAuthentication": { + "Audience": "account", + "Host": "http://192.168.0.10:8321/realms/Persistence" + }, "AuthUser": { "username": "myuser", "password": 12345,