I am currently trying to write AuthenticationMiddleware. See this answer . The application works without errors, but when I execute dnx web, I get the following error:
Unable to find a suitable constructor for type 'Namespace.BasicAuthenticationMiddleware'. Make sure that the type is specific and that all parameters are accepted by the constructor.
at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance (IServiceProvider provider, type instanceType, Object [])
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 am sure that the constructor signature I use is wrong, but I can’t find the appropriate documentation for this, as it seems that there are dozens of obsolete ones.
This is the AuthenticationMiddleware property:
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)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthorization(options => {
options.AddPolicy(BasicAuthOptions.Scheme, policy => policy.Requirements.Add(new BasicAuthRequirement()));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseBasicAuthentication();
app.UseMvc();
}
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}