'ASP.NET Core 6 identityserver (duende) : no DefaultAuthenticateScheme found or no CookieAuthenticationScheme configured on IdentityServerOptions

So here's our setup. we have

  • Angular web app
  • Identity server API (ASP.NET Core 6, duende identity server)

Identity server config in Program.cs:

    builder.Services
        .AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    builder.Services
        .AddIdentityServer(options =>
        {
            options.Events.RaiseErrorEvents = true;
            options.Events.RaiseInformationEvents = true;
            options.Events.RaiseFailureEvents = true;
            options.Events.RaiseSuccessEvents = true;

            // see https://docs.duendesoftware.com/identityserver/v6/fundamentals/resources/
            options.EmitStaticAudienceClaim = true;
            options.IssuerUri = configuration["IssuerUri"];
        })
        .AddConfigurationStore(options =>
        {
            options.ConfigureDbContext = b =>
                b.UseMySql(conString, serverVersion, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly));
        })
        .AddOperationalStore(options =>
        {
            options.ConfigureDbContext = b =>
            {
                b.UseMySql(conString, serverVersion, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly));
            };
        })
        .AddAspNetIdentity<ApplicationUser>();
    
    builder.Services.AddAuthentication(options => {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultForbidScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
return builder.Build();

Then after that, we call

app.useAuthorization();
    app.useAuthentication();

When the web app is trying to get the openid configuration from the identity server url (/.well-known/openid-configuration), we are getting this error:

System.InvalidOperationException: No DefaultAuthenticateScheme found or no CookieAuthenticationScheme configured on IdentityServerOptions.

at Microsoft.AspNetCore.Http.AuthenticationManagerExtensions.GetCookieAuthenticationSchemeAsync(HttpContext context) in //src/IdentityServer/Extensions/HttpContextAuthenticationExtensions.cs:line 54
at Duende.IdentityServer.Services.DefaultUserSession.AuthenticateAsync() in /
/src/IdentityServer/Services/Default/DefaultUserSession.cs:line 135
at Duende.IdentityServer.Services.DefaultUserSession.GetSessionIdAsync() in //src/IdentityServer/Services/Default/DefaultUserSession.cs:line 215
at Duende.IdentityServer.Services.DefaultUserSession.EnsureSessionIdCookieAsync() in /
/src/IdentityServer/Services/Default/DefaultUserSession.cs:line 226
at Duende.IdentityServer.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events, IIssuerNameService issuerNameService, IBackChannelLogoutService backChannelLogoutService) in //src/IdentityServer/Hosting/IdentityServerMiddleware.cs:line 55
at Duende.IdentityServer.Hosting.MutualTlsEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes) in /
/src/IdentityServer/Hosting/MutualTlsEndpointMiddleware.cs:line 94
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Duende.IdentityServer.Hosting.DynamicProviders.DynamicSchemeAuthenticationMiddleware.Invoke(HttpContext context) in //src/IdentityServer/Hosting/DynamicProviders/DynamicSchemes /DynamicSchemeAuthenticationMiddleware.cs:line 47
at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.g__InvokeCoreAwaited|15_0(HttpContext context, Task`1 policyTask)
at Duende.IdentityServer.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) in /
/src/IdentityServer/Hosting/BaseUrlMiddleware.cs:line 27
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)



Solution 1:[1]

I checked your codes and found two mistakes:

1,if you are using cookie authentication,and setted as below: enter image description here

you need to modify your codes as below:

    builder.Services.AddAuthentication(options => 
     {
        ......
     })
   .AddCookie( "Cookies",options =>
    {
        ......;
    })

2,you need to move the codes:

app.useAuthentication();

in front of

app.useAuthorization(); 

You could modify your codes and try again,and if you could share more codes related,I could test for you

Update : try to set your IdentityServer Auth Options:

builder.Services
        .AddIdentityServer(options =>
        {
            ......
            options.Authentication.CookieAuthenticationScheme= ....
            options.Authentication.CookieLifetime = .....
            .....
        }); 

You could follow the document: http://docs.identityserver.io/en/latest/reference/options.html#authentication

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1