ASP.NET Core 6 IdentityServer(DUENDE):未找到defaultauthenticatescheme或在IdentityServeroptions上配置的cookieAthenticationscheme

发布于 2025-01-24 17:03:45 字数 4476 浏览 0 评论 0 原文

所以这是我们的设置。我们拥有

  • Angular Web应用程序
  • Identity Server API(ASP.NET Core 6,Duende Identity Server)

Indentity Server在 program.cs.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();

然后,我们

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

在Web应用程序试图获取OpenID时 呼叫从身份服务器URL(/.well-newand/openid-configuration)配置的配置,我们遇到此错误:

system.invalidoperationException:未找到defaultauthenticatescheme或在IdentityServeroptions上配置的cookiuthenticationsCheme。

在Microsoft.aspnetcore.http.authenticationmanagerextensions.getCookieAuthentIcationsChemeAsync(httpcontext Context)in//src/indistityserver/entistityserver/extensions/htttpcontextextextextextectextextextensions.cs.cs.cs.cs.cs.cs.cs: 在duende.IdentityServer.Services.defaultUsersession.authentIcateAsync()中in//>/>/>/>/>/>/>/ 在duende.indistityserver.services.defaultusessession.getSessionIdasync()in//src/sidentityserver/services/default/default/default/defaultusersession.cs:line 215
在duende.IdentityServer.Services.DefaultUsersessessessessessessessessessessessessessesseessionIdCookieAsync()中 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
在duende.IdentityServer.hosting.mutualtlsendpointmiddleware.invoke(httpcontext上下文,iauthenticationschemeprovider schemes)in//
/src/indentityserityserver/sidentityserver/hosterver/hosting/mutualtlsendpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointpointmmiddleware.cselline 94
在microsoft.aspnetcore.authentication.authenticationmiddleware.invoke(httpcontext上下文)
上 在///src/sidentityServer/hosting/hosting/hosting/dynamedicproviders/dynamecproviders/dynamicicschemeschemeschemicschemicschemicschemicschemeachemeachemeateenticationmetple.cs-css in Duende.IdentityServer.hoster.hosting.Hosting.DynamicProviders.DynamicsCheMeAuthenticationmiddle.Invoke.Invoke(httpContext Context) 在microsoft.aspnetcore.cors.infrastructure.corsmiddle.g__invokecoreawaited | 15_0(httpcontext上下文,task`1 polighttask)

在duende.indeityserver.hosting.baseurlmiddleware.invoke(httpcontext上下文)中 在Microsoft.aspnetcore.diagnostics.developerexceptionpagemiddle.invoke(httpcontext上下文)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2025-01-31 17:03:45

我检查了您的代码并发现了两个错误:

1,如果您使用的是Cookie Authentication,并设置为以下内容:

您需要修改您的代码如下:

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

2,您需要移动代码:

app.useAuthentication();

在您面前

app.useAuthorization(); 

可以修改代码并重试,如果您可以共享有关更多代码,我可以测试为您

更新:
尝试设置您的IdentityServer auth选项:

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

您可以关注文档:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文