bielu000
bielu000

Reputation: 2241

Why authentication is failed after successfulyy validate token?

I've created pretty simple IdentityServer based on OpenIdConnect concept. My identity server after authentication returns to my app JwtToken. When I trying to get access to authorized route, I have following logs:

Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2]

Successfully validated the token.

but below I have:

Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler[7] OpenIdConnect was not authenticated. Failure message: Not authenticated

and I'm redirected to auth route in my IdentityServer.

Why is that?

Here is my client(not IdentityServer) configuration:

    services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.IncludeErrorDetails = true;
            options.RequireHttpsMetadata = false;
            options.Audience = "http://localhost:5001";
            options.MetadataAddress = "http://localhost:5000/.well-known/openid-configuration";
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidIssuer = "http://localhost:5000"
            };
        })
        .AddOpenIdConnect(options =>
        {
            options.SignInScheme = JwtBearerDefaults.AuthenticationScheme;
            options.AuthenticationMethod = OpenIdConnectRedirectBehavior.FormPost;
            options.MetadataAddress = "http://localhost:5000/.well-known/openid-configuration";
            options.RequireHttpsMetadata = false;
            options.ClientId = "user";
            options.ClientSecret = "secret";
            options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
            options.SaveTokens = true;
        });

Upvotes: 0

Views: 2641

Answers (1)

j-hurst
j-hurst

Reputation: 329

If you just need to authenticate off of bearer tokens with IdentityServer, look at using the IdentityServer4.AccessTokenValidation nuget package.

Your startup would look something like this:

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
    .AddIdentityServerAuthentication(options =>
    {
      options.Authority = "IdentityServerURL";
      options.ApiName = "apiScope";
    });

Upvotes: 1

Related Questions