diff --git a/src/Keycloak.AuthServices.Authentication/WebAppExtensions/KeycloakWebAppServiceCollectionExtensions.cs b/src/Keycloak.AuthServices.Authentication/WebAppExtensions/KeycloakWebAppServiceCollectionExtensions.cs index 89403fcb..d90e6fd8 100644 --- a/src/Keycloak.AuthServices.Authentication/WebAppExtensions/KeycloakWebAppServiceCollectionExtensions.cs +++ b/src/Keycloak.AuthServices.Authentication/WebAppExtensions/KeycloakWebAppServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -namespace Keycloak.AuthServices.Authentication; +namespace Keycloak.AuthServices.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; @@ -46,4 +46,43 @@ public static KeycloakWebAppAuthenticationBuilder AddKeycloakWebAppAuthenticatio displayName ); } + + /// + /// Add authentication with Keycloak + /// This method expects the configuration file will have a section, (by default named "Keycloak"), with the necessary settings to + /// initialize the authentication options. + /// + /// Service collection to which to add authentication. + /// The IConfiguration object. + /// The name of the configuration section with the necessary + /// settings to initialize authentication options. + /// Optional name for the open id connect authentication scheme + /// (by default OpenIdConnectDefaults.AuthenticationScheme). This can be specified when you want to support + /// several OpenIdConnect identity providers. + /// Optional name for the cookie authentication scheme + /// (by default CookieAuthenticationDefaults.AuthenticationScheme). + /// /// + /// A display name for the authentication handler. + /// The authentication builder to chain extension methods. + public static KeycloakWebAppAuthenticationBuilder AddKeycloakWebAppAuthentication( + this IServiceCollection services, + IConfiguration configuration, + string configSectionName = KeycloakAuthenticationOptions.Section, + string openIdConnectScheme = OpenIdConnectDefaults.AuthenticationScheme, + string cookieScheme = CookieAuthenticationDefaults.AuthenticationScheme, + Action? configureOpenIdConnectOptions=null, + string? displayName = null + ) + { + var builder = services.AddAuthentication(openIdConnectScheme); + + return builder.AddKeycloakWebApp( + configuration.GetRequiredSection(configSectionName), + null, + configureOpenIdConnectOptions, + openIdConnectScheme, + cookieScheme, + displayName + ); + } } diff --git a/tests/TestWebApi/Properties/launchSettings.json b/tests/TestWebApi/Properties/launchSettings.json new file mode 100644 index 00000000..32ca6834 --- /dev/null +++ b/tests/TestWebApi/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "TestWebApi": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:19494;http://localhost:19495" + } + } +} \ No newline at end of file diff --git a/tests/TestWebApiWithControllers/Properties/launchSettings.json b/tests/TestWebApiWithControllers/Properties/launchSettings.json new file mode 100644 index 00000000..18f5911b --- /dev/null +++ b/tests/TestWebApiWithControllers/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "TestWebApiWithControllers": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:19496;http://localhost:19497" + } + } +} \ No newline at end of file