Skip to content

AuthServices

IOF edited this page Nov 28, 2025 · 2 revisions
public static class AuthServices
{
    public static IServiceCollection AddAuthServices(
        this IServiceCollection services,
        IConfiguration config
    )
    {
        services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Authority = $"{config["KeyCloakHost"]}/realms/ModulBankApp";
                options.Audience = "backend-api";
                options.RequireHttpsMetadata = false;
                options.MetadataAddress =
                    $"{config["KeyCloakHost"]}/realms/ModulBankApp/.well-known/openid-configuration";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience = true,
                    ValidAudiences = ["backend-api"],

                    ValidateLifetime = true,

                    ValidateIssuer = true,
                    ValidIssuers = [$"{config["KeyCloakHost"]}/realms/ModulBankApp"],
                    ValidateIssuerSigningKey = true,
                };
            });

        return services;
    }
}

Clone this wiki locally