Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler 证书验证失败

Posted

技术标签:

【中文标题】Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler 证书验证失败【英文标题】:Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler Certificate validation failed 【发布时间】:2022-01-15 07:20:20 【问题描述】:

我在生产验证证书时遇到了一些问题。我有以下设置。使用以下代码在 ASP.NET Core 中实现 Kestrel Web 服务器

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                
                    string pass = @"my_complicated_password";
                    var cert = new X509Certificate2(@"C:\certs\my_server_cert.pfx", pass);

                    webBuilder.ConfigureKestrel(o =>
                    
                        o.ConfigureHttpsDefaults(defaults =>
                        
                            defaults.ServerCertificate = cert;
                            defaults.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
                            defaults.AllowAnyClientCertificate();
                        );
                    );

                    webBuilder.UseKestrel(options =>
                    
                        options.ListenAnyIP(8384, o =>
                        
                            o.UseHttps();
                        );
                    );

                    webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                    webBuilder.UseStartup<Startup>();

                );
    

现在当我在本地运行时,我看到以下内容

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'https://localhost:8384'. Binding to endpoints defined in UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://[::]:8384
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\location_of_web_app
crit: MyNameSpace.CertificateValidationService[1]
      WE ARE IN THE VALIDATE CERT SECTION
crit: MyNameSpace.CertificateValidationService[2]
      THE CERT BEING PASSED WAS THE_PERSON```

这是我所期望的。但是,在生产中,我看到以下内容:

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://[::]:8384
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\my_app_location
warn: Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler[2]
      Certificate validation failed, subject was [All the stuff 
      PartialChain A certificate chain could not be built to a trusted root authority.
      RevocationStatusUnknown The revocation function was unable to check revocation for the certificate.
      OfflineRevocation The revocation function was unable to check revocation because the revocation server was offline.

我正在尝试确定缺少的步骤是什么,但这有点超出我的专业知识

【问题讨论】:

检查下发客户端证书的 CA 机构,然后确保该机构在服务器的 Trusted Root Certification Authorities 中受信任。也许您在本地计算机上信任此 CA,或者您在本地使用的 CA 的客户端证书受您的计算机信任(或自签名信任)。但是如果服务器接受任何客户端证书,您必须信任客户端证书的 CA。如果 CA 在服务器本地不受信任,则服务器应该能够连接到 AIA 端点包括以确保证书有效。 @Hazrelle 谢谢你 - 只是为了澄清。您是说去生产服务器(此应用程序所在的位置)并验证 .pfx 文件和整个链是否显示在“信任根证书颁发机构”中?基本上,我正在尝试做的事情可能是错误的。使用 defaults.ServerCertificate = cert 使网站显示连接是安全的。然后用户使用不同的方法进行身份验证(在这种情况下通过 CARD 上的 X509 证书)并且应该命中 CertValidationService 谢谢 - 大声说出来让我意识到我没有 CARD 授权链。上帝保佑 服务器端应该没问题。生产服务器不信任的是客户端证书链:) 你是对的 【参考方案1】:

正如 Hazrelle 所发布的,问题在于生产服务器上的客户端证书链配置不正确。

【讨论】:

以上是关于Microsoft.AspNetCore.Authentication.Certificate.CertificateAuthenticationHandler 证书验证失败的主要内容,如果未能解决你的问题,请参考以下文章