0
votes

i'm using using identityserver token to validate and getting error "No authentication handlers are registered. Did you forget to call AddAuthentication().AddSomeAuthHandler?"

Below is my code

Web API startup.cs:

public void ConfigureServices(IServiceCollection services) {

        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();

Client Side startup.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();

Please help me to fix my code error Thanks

1

1 Answers

0
votes

You need to add the following two lines in your Startup.Configure method:

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

Do add them after the useRouting() statement.