From 218eb6520199f22f8fd87ff2cc18160dcb4cbe4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D1=8F=20=D0=91=D0=B8=D0=B7=D1=8E=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0?= Date: Tue, 17 Dec 2024 10:58:30 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BA=20Keycloak=20=D0=B2?= =?UTF-8?q?=20appsettings.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.API/DependencyInjection.cs | 10 ++++++---- DD.Persistence.App/appsettings.json | 9 ++++----- DD.Persistence.App/defaultsettings.json | 9 ++++----- 3 files changed, 14 insertions(+), 14 deletions(-) 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,