[英]Asp.Net: No suitable Constructor for AuthenticationMiddleware
I'm currently trying to write an AuthenticationMiddleware. 我目前正在尝试编写AuthenticationMiddleware。 See this answer . 看到这个答案 。 The app builds with no error but when I execute dnx web
I get the following error: 应用程序构建没有错误,但当我执行dnx web
我收到以下错误:
Unable to locate suitable constructor for type 'Namespace.BasicAuthenticationMiddleware'. 无法找到“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) 在Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider,Type instanceType,Object []参数)
at Microsoft.AspNet.Builder.UseMiddlewareExtensions.<>c__DisplayClass2_0.b__0(RequestDelegate next) 在Microsoft.AspNet.Builder.UseMiddlewareExtensions。<> c__DisplayClass2_0.b__0(RequestDelegate next)
at Microsoft.AspNet.Builder.Internal.ApplicationBuilder.Build() 在Microsoft.AspNet.Builder.Internal.ApplicationBuilder.Build()
at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication() 在Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()
fail: Microsoft.AspNet.Hosting.Internal.HostingEngine[7] 失败: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: 这是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: BasicAuthOptions:
public class BasicAuthOptions : AuthenticationOptions {
public const string Scheme = "BasicAuth";
public BasicAuthOptions()
{
AuthenticationScheme = Scheme;
AutomaticAuthenticate = true;
}
}
BasicAuthenticationExtensions BasicAuthenticationExtensions
public static class BasicAuthenticationExtensions
{
public static void UseBasicAuthentication(this IApplicationBuilder builder) {
builder.UseMiddleware<BasicAuthenticationMiddleware>(new ConfigureOptions<BasicAuthOptions>(o => new BasicAuthOptions()));
}
}
Startup.cs: 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);
}
Your UseBasicAuthentication
extension tries to inject a ConfigureOptions
instance that your middleware doesn't take as a parameter. 您的UseBasicAuthentication
扩展尝试注入您的中间件不作为参数的ConfigureOptions
实例。
Simply flow the options instance as-is: 只需按顺序流动选项实例:
public static class BasicAuthenticationExtensions {
public static void UseBasicAuthentication(this IApplicationBuilder builder) {
builder.UseMiddleware<BasicAuthenticationMiddleware>(new BasicAuthOptions());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.