Ocelot: [Question] How to add custom authentication in ocelot

Created on 2 Jul 2018  路  11Comments  路  Source: ThreeMammals/Ocelot

Ocelot support to custom authen ?
I try to add my custom authen to ocelot but can't work.
This error

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

question

All 11 comments

@kasitimo thanks for your interest in the project! Please can you upload your source code so I can have a closer look?

If not you need to add a authentication scheme with you CustomScheme key as per the docs http://ocelot.readthedocs.io/en/latest/features/authentication.html

@kasitimo try setting the authentication in program.cs I will take a closer look ASAP.

@TomPallister please guide to setting authentication in program.cs

@kasitimo change your program.cs to this and Ocelot will start and delete Startup.cs your code doesnt use it.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Ocelot.Middleware;
using Ocelot.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using APIGateway.Authentication;
using Microsoft.AspNetCore.Builder;

namespace APIGateway
{
    public class Program
    {
        public static void Main(string[] args)
        {
            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")
                       .AddEnvironmentVariables();
               })
               .ConfigureServices(s =>
               {
                   s.AddAuthentication(options =>
                    {
                        options.DefaultScheme = "CustomAuth";
                        //options.DefaultAuthenticateScheme = "CustomScheme";
                        //options.DefaultChallengeScheme = "CustomScheme";
                    })
                    .AddCustomAuth("CustomAuth", o => { });
                    s.AddOcelot();
               })
               .ConfigureLogging((hostingContext, logging) =>
               {
                   //add your logging
               })
               .UseIISIntegration()
               .Configure(app =>
               {
                   app.UseAuthentication();
                   app.UseOcelot().Wait();
               })
               .UseUrls("http://localhost:9000")
               .Build()
               .Run();
        }
    }
}

It work!!
Thank you for your help

@kasitimo no problem :)

@TomPallister I have some question ocelot support multiple AuthenticationProviderKey in AuthenticationOptions ?

eg.
"AuthenticationOptions": {
"AuthenticationProviderKey": ["Auth2","Auth2"]
}

@kasitimo not at the moment, Im not sure if asp.net core can do this!

.AddCustomAuth("CustomAuth", o => { });

When I apply your code I got an error on that line of code
"IServiceCollection" does not contain a definition for "AddCustomAuth"

@TomPallister i have a doubt the above same code doesn't work with Azure AD JWT provider with "AzureAdJWTBearer" as provider key, it says unsupported authentication provider key and doesn't start the ocelot.

Was this page helpful?
0 / 5 - 0 ratings