IdentityServer 未注册身份验证处理程序

问题描述 投票:0回答:1

我正在使用 Identityserver 令牌进行验证并收到错误“未注册任何身份验证处理程序。您是否忘记调用 AddAuthentication().AddSomeAuthHandler?”

下面是我的代码

Web API 启动.cs:

公共无效ConfigureServices(IServiceCollection服务) {

        var config = Configuration.GetSection("AuthSettings").Get<AuthSettings>();
        services.AddAuthentication(
            options =>
        {
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

        }
        ).AddJwtBearer(o =>
        {
            o.Authority = config.AuthUrl;
            o.Audience = config.AuthAudience; 
            o.RequireHttpsMetadata = false;
            o.SaveToken = true;
            //o.TokenValidationParameters = tokenValidationParameters;
            //o.Configuration = new OpenIdConnectConfiguration();  
            o.BackchannelHttpHandler = new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                Proxy = new WebProxy(Configuration["System:Proxy"])
            };
        });
        //services.AddAuthorization(options =>
        //{
        //    options.AddPolicy("PublicSecure", policy => policy.RequireClaim("client_id", config.ClientId));
        //});
        services.AddAuthorization();

客户端启动.cs:

var authConfig = Configuration.GetSection("AppSettings").Get();

        // Adds an instance of the class that contains credentials
        services.AddSingleton(new ClientCredentialsTokenRequest
        {
            Address = authConfig.AuthURL,
            ClientId = authConfig.AuthClientId,
            ClientSecret = authConfig.AuthClientSecret,
            Scope = authConfig.AuthScope
        });

        services.AddAuthentication();

请帮助我修复我的代码错误 谢谢

asp.net-mvc asp.net-core authentication identityserver4
1个回答
2
投票

您需要在 Startup.Configure 方法中添加以下两行:

app.UseAuthentication();
app.UseAuthorization();

请在

useRouting()
语句之后添加它们。

© www.soinside.com 2019 - 2024. All rights reserved.