gpt4 book ai didi

c# - Asp.Net:没有适合 AuthenticationMiddleware 的构造函数

转载 作者:太空狗 更新时间:2023-10-30 00:49:48 26 4
gpt4 key购买 nike

我目前正在尝试编写一个 AuthenticationMiddleware。参见 this answer .该应用程序构建没有错误,但是当我执行 dnx web 时,出现以下错误:

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]

我确定我使用的构造函数签名在某些方面是错误的,但我找不到合适的文档,因为似乎有许多已弃用的文档。

这是 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();
}
}

基本授权选项:

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

基本身份验证扩展

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

启动.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);
}

最佳答案

您的 UseBasicAuthentication 扩展试图注入(inject)您的中间件不作为参数的 ConfigureOptions 实例。

简单地按原样流动选项实例:

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

关于c# - Asp.Net:没有适合 AuthenticationMiddleware 的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36100492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com