Ocelot icon indicating copy to clipboard operation
Ocelot copied to clipboard

Unable to start Ocelot, errors are: Authentication Options AuthenticationProviderKey:TestKey,AllowedScopes:[] is unsupported authentication provider

Open BorisSokolov opened this issue 5 years ago • 9 comments

Expected Behavior / New Feature

Ocelot starts and works with IdentityServer4 Authentication

Actual Behavior / Motivation for New Feature

Ocelot crashes with:

Unable to start Ocelot, errors are: Authentication Options AuthenticationProviderKey:TestKey,AllowedScopes:[] is unsupported authentication provider image

Steps to Reproduce the Problem

  1. Follow the steps for IdentityServer 4 Config looks like:
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44309
        }
      ],
      "UpstreamPathTemplate": "/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "TestKey",
        "AllowedScopes": []
      }
    }

Startup:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddControllers();
            ConfigureIdentityServer(services);
            services.AddOcelot();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseHttpsRedirection();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            //app.UseEndpoints(endpoints =>
            //{
            //    endpoints.MapControllers().RequireAuthorization();
            //});

            app.UseOcelot().Wait();
        }

        private void ConfigureIdentityServer(IServiceCollection services)
        {
            //IdentityServerConfig identityServerConfig = new IdentityServerConfig();
            //Configuration.Bind("IdentityServerConfig", identityServerConfig);
            var authenticationProviderKey = "TestKey";

            services.AddAuthentication()
                .AddIdentityServerAuthentication(authenticationProviderKey, options =>
                {
                    options.RequireHttpsMetadata = false;
                    options.Authority = $"http://localhost:5000";
                    options.ApiName = "api1";
                }
                );
        }

Program

    public class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
               .Enrich.FromLogContext()
               .MinimumLevel.Debug()
               .WriteTo.Console(
                   LogEventLevel.Verbose,
                   "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
                   .CreateLogger();

            try
            {
                CreateHostBuilder(args).Build().Run();
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IWebHostBuilder CreateHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config
                    .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                    .AddJsonFile("ocelot.json")
                    .AddEnvironmentVariables();
            })
            .ConfigureServices(s =>
            {
                s.AddOcelot();
                s.AddMvc();
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                //add your logging
                logging.AddSerilog();
            })
            .Configure(a =>
            {
                a.UseOcelot().Wait();
            });
    }
  1. Run the application

Specifications

  • Version: Ocelot 13.8.0
  • Platform: .Net Core 3.0 on Windows 10 x64
  • Subsystem:

BorisSokolov avatar Jan 13 '20 22:01 BorisSokolov

Would you mind sharing a repro of this issue so we can investigate what's going wrong here?

jmezach avatar Feb 19 '20 10:02 jmezach

any updates on this issue? I am getting this on .Net core 3 api.

BHARAT703 avatar Apr 22 '20 11:04 BHARAT703

I had this issue because of my appsettings.<environment>.json file wasn't getting imported properly so therefore the ocelot had no matching configuration. I was importing my config as an ExpandoObject and c# didn't like that one of my properties was an array. So I changed it from an array to a serialzed string array and my code was happy.

PostImpatica avatar May 29 '20 19:05 PostImpatica

In my case, I called the wrong .AddJwtBearer without the provider key like this:

services.AddAuthentication()
        .AddJwtBearer(x =>
        {
            x.Authority = "test";
            x.Audience = "test";
        });

Should have been

services.AddAuthentication()
        .AddJwtBearer("TestKey", x =>
        {
            x.Authority = "test";
            x.Audience = "test";
        });

natiox avatar Jun 01 '20 17:06 natiox

@BorisSokolov & @jmezach did you guys found any solution around it?

jawand avatar Sep 08 '20 12:09 jawand

@jawand, nope :) I am currently switched to another project that is not using Ocelot.

BorisSokolov avatar Sep 10 '20 20:09 BorisSokolov

I've run into this issue as well.

Following the example in the Ocelot docs produces this error for me

metalrose24 avatar Oct 16 '20 16:10 metalrose24

I manged to solve this. For me, I had configuration in both Startup.cs and Program.cs. It wasn't using my Startup.cs for the Ocelot config. If your following the samples then the configuration for authentication needs to be under the .ConfigureServices part of the WebHostBuilder e.g. in Program.cs Main method:

        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config
                    .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                    .AddJsonFile("ocelot.json", false, true)
                    .AddEnvironmentVariables();
            })
            .ConfigureServices(services =>
            {
                void Options(IdentityServerAuthenticationOptions o)
                {
                    o.Authority = authenticationServerBaseAddress;
                    o.ApiName = ServiceClientId;
                    o.SupportedTokens = SupportedTokens.Both;
                    o.ApiSecret = ServiceSecret;
                }

                services.AddAuthentication()
                    .AddIdentityServerAuthentication(authenticationProviderKey, Options);

                services.AddOcelot();
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                //add your logging TODO
            })
            .UseIISIntegration()
            .Configure(app =>
            {
                app.UseOcelot().Wait();
            })
            .Build()
            .Run();

I found it useful to actually take a copy of the code so I could debug through it then it made more sense what it was doing and why it wasn't picking up my authentication config

Hope this helps someone !

metalrose24 avatar Oct 21 '20 13:10 metalrose24

I had it too because my SymmetricSecurityKey does not loaded correctly from database in ConfigureServices() method.

Iran-asp avatar Nov 30 '21 14:11 Iran-asp

@BorisSokolov commented on Sep 10, 2020:

@jawand, nope :) I am currently switched to another project that is not using Ocelot.

Орёл ты - Соколов! Орёл!

raman-m avatar Mar 23 '24 10:03 raman-m

The author doesn't care!

Misconfiguration issue of the startup code where required authentication provider was not registered!

raman-m avatar Mar 23 '24 10:03 raman-m