从 .NET Core 2.2 移动到 NET 5 时出现关联失败错误

问题描述

我使用 IdentityServer3 进行 OpenIDConnect 身份验证。我有一个 ASP.NET Core 2.2 客户端应用程序,它在 startup.cs 中使用以下代码进行身份验证,并且运行良好。

public void ConfigureServices(IServiceCollection services)
{
        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,options =>
        {
            options.LoginPath = "/Home";
            options.AccessDeniedPath = "/Account/AccessDenied";
            options.Cookie = new CookieBuilder()
            {
                Name = "MyAuthCookie",HttpOnly = true,};
            options.SlidingExpiration = true;
        })
        .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme,options =>
        {
            options.Authority = configuration["Identity:Authority"];
            options.ClientId = configuration["Identity:ClientId"];
            options.ResponseType = "id_token";
            options.CallbackPath = "/Home";
            options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.SignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.Scope.Add(OpenIdConnectScope.Email);
            options.Events = new OpenIdConnectEvents()
            {
                OnTokenValidated = async context =>
                {
                    // removed for brevity purpose                        
                },OnRedirectToIdentityProvider = async context =>
                {
                    if (context.ProtocolMessage.RequestType == OpenIdConnectRequestType.Authentication)
                    {
                        context.ProtocolMessage.AcrValues = "tenant:" + context.Request.Host.Value;
                    }
                    await Task.FromResult(0);
                },OnRedirectToIdentityProviderForSignOut = async context =>
                {
                    context.ProtocolMessage.IdTokenHint = context.HttpContext.User.FindFirst("id_token").Value;
                    await Task.FromResult(0);
                }
            };
        });
}

我有一个使用 .NET 5 的新应用程序,其代码与上述相同。 但是,在 NET 5 应用程序中,在用户成功登录 IndentityServer 以及身份服务器重定向回客户端应用程序后,我收到异常。

enter image description here

.NET 5 发生了什么变化?

其他不同之处在于 Home 路由的设置方式。不知道这是否会有所不同

在 .NET 核心 2.2 中

app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );

                routes.MapRoute(
                    name: "default",template: "{controller=Home}/{action=Index}/{id?}");
}

.NET 5

  app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
             name: "areas",pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
           );

            endpoints.MapControllerRoute(
                name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
      }

解决方法

找到了。 我在 VS 2019 中进行了本地测试。我启用了 SSL 并且它工作正常。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...