4

I'm currently trying to write an AuthenticationMiddleware. See this answer. The app builds with no error but when I execute dnx web I get the following error:

Unable to locate suitable constructor for type 'Namespace.BasicAuthenticationMiddleware'. Ensure the type is concrete and all parameters are accepted by a constructor.

at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)

at Microsoft.AspNet.Builder.UseMiddlewareExtensions.<>c__DisplayClass2_0.b__0(RequestDelegate next)

at Microsoft.AspNet.Builder.Internal.ApplicationBuilder.Build()

at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()

fail: Microsoft.AspNet.Hosting.Internal.HostingEngine[7]

I'm sure that the Constructor signature I use is wrong in some way but I'm not able to find a suitable documentation for this, since it seems like there are dozens of deprecated ones.

This is the AuthenticationMiddleware:

public class BasicAuthenticationMiddleware : AuthenticationMiddleware<BasicAuthOptions>
{
    public BasicAuthenticationMiddleware(
        RequestDelegate next, 
        BasicAuthOptions options, 
        ILoggerFactory loggerFactory, 
        IUrlEncoder urlEncoder)
        : base(next, options, loggerFactory, urlEncoder) {}

    protected override AuthenticationHandler<BasicAuthOptions> CreateHandler()
    {
        return new BasicAuthenticationHandler();
    }
}

BasicAuthOptions:

public class BasicAuthOptions : AuthenticationOptions {
    public const string Scheme = "BasicAuth";
    public BasicAuthOptions()
    {
        AuthenticationScheme = Scheme;
        AutomaticAuthenticate = true;
    }
}

BasicAuthenticationExtensions

public static class BasicAuthenticationExtensions
{
    public static void UseBasicAuthentication(this IApplicationBuilder builder) {
        builder.UseMiddleware<BasicAuthenticationMiddleware>(new ConfigureOptions<BasicAuthOptions>(o => new BasicAuthOptions()));
    }
}

Startup.cs:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();

        services.AddAuthorization(options => {
            options.AddPolicy(BasicAuthOptions.Scheme, policy => policy.Requirements.Add(new BasicAuthRequirement()));
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseIISPlatformHandler();

        app.UseStaticFiles();

        app.UseBasicAuthentication();

        app.UseMvc();
    }

    // Entry point for the application.
    public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
Community
  • 1
  • 1
Sebastian Krogull
  • 1,361
  • 1
  • 13
  • 30
  • Here is where you're throwing. https://github.com/aspnet/HttpAbstractions/blob/02363da94eee7311e457e136dc45edf9c5ac3494/src/Microsoft.AspNetCore.Http.Abstractions/Extensions/UseMiddlewareExtensions.cs#L75 – Shaun Luttin Mar 19 '16 at 14:34

1 Answers1

6

Your UseBasicAuthentication extension tries to inject a ConfigureOptions instance that your middleware doesn't take as a parameter.

Simply flow the options instance as-is:

public static class BasicAuthenticationExtensions {
    public static void UseBasicAuthentication(this IApplicationBuilder builder) {
        builder.UseMiddleware<BasicAuthenticationMiddleware>(new BasicAuthOptions());
    }
}
Kévin Chalet
  • 33,128
  • 7
  • 104
  • 124