13
votes

I am learning about the IdentityServer4 https://identityserver4.readthedocs.io/en/release/quickstarts/0_overview.html but seem to be having issues getting it working.

After creating the API section of the project and configured accordingly:

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
            {
                Authority = "http://localhost:5000",
                RequireHttpsMetadata = false,

                ApiName = "api1"
            });

            app.UseMvc();
        }

I get the following error:

'IApplicationBuilder' does not contain a definition for 'UseIdentityServerAuthentication' and no extension method 'UseIdentityServerAuthentication' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

Any advice what i am missing here?

EDIT 1: I've noticed i cannot reference the namespace either even tho the package is installed and present.

i.e.

using IdentityServer4.AccessTokenValidation;

or

using IdentityServer4;
6
Which version of Visual Studio are you using? I am using VS 2017. I had the same issue. However, it would build even though it was showing this issue. I closed and opened VS 2017 as @Jeremy Cook suggested and the issue went away.mmcfly
Vs2017. Had same issue in vs2015. Restarting ide didn't helpAeseir

6 Answers

22
votes

Well, I just closed and reopened Visual Studio 2017, and the problem went away. Perhaps Visual Studio choked on pulling down the IdentityServer4.AccessTokenValidation package the first time, and reopening the project caused it to try again and this time it succeeded.

20
votes

If you migrate from net core 1.1 to 2.0, you need to migrate your code as described in Identity Server: API Migration to ASP.NET Core 2

In the Configure function

Before:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:5000",
    RequireHttpsMetadata = false,
    ApiName = "apiApp"
});

After:
app.UseAuthentication();

In the ConfigureServices function

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = 
                               JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = 
                               JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
    o.Authority = "http://localhost:5000";
    o.Audience = "apiApp";
    o.RequireHttpsMetadata = false;
});
11
votes

You need this package. And use the namespace IdentityServer4.AccessTokenValidation

5
votes

I had the same issue and I solved by following IdentityServer 4 Quickstart Samples, in particular the Javascript Client sample.

So, this is what it worked here:

  1. Check dependencies versions. There are issues reported for IdentityServer running with .NET Core 2.0. In my API project I used Microsoft.Asp.NetCore.All version 2.0.5 and IdentityServer4.AccessTokenValidation version 2.3.0.

  2. Inside Startup.cs, at Configure method add: app.UseAuthentication(); as it is used in the example.

  3. The last step is to add authentication to ConfigureServices method as it is in the example:

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddMvcCore()
        .AddAuthorization()
        .AddJsonFormatters();
    
      services.AddAuthentication("Bearer")
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "http://localhost:5000";
            options.RequireHttpsMetadata = false;
    
            options.ApiName = "api1";
        });
     }
    

And that solved in my case. Hopefully it works for you too, just let me know how it goes.

Happy coding.

0
votes

Just to provide update/answer, the subsequent version of IdentityServer4 packages worked fine.

Currently using ID4 2.0 packages.

-1
votes

You can check the latest documentation at github IdentityServer4:

It sets the authentication options in the ConfigureServices method using the services.AddAuthentication method.

Here is the test server code they are using